This project is read-only.

This project has been developed to read list and libraries. Any feedback will be appreciated.

 

This project has been developed to read/write list and libraries. To Get list items as your custom entity, your class must be inherited from EntityBase.

The list below has been created for sample and it includes most of common used SP fileds.

 List

public class AllFieldsListEntity : EntityBase

{

        [SPFieldFlag]

        public string MultipleLineField { get; set; }

        //Field's name is different form property name

        //This field's return type is 'string'. But it can be changed by developing to return SPFieldMultiChoiceValue

        [SPFieldFlag(Name = "ChoiceField")]

        public string MyChoices { get; set; }

       //Field created as 0 decimal

        [SPFieldFlag]

        public int NumberField { get; set; }

        [SPFieldFlag]

        public double CurrencyField { get; set; }

        [SPFieldFlag]

        public DateTime DateTimeField { get; set; }

        [SPFieldFlag]

        public SPFieldLookupValue LookupField { get; set; }

        [SPFieldFlag]

        public bool YesNoField { get; set; }

        [SPFieldFlag]

        public SPFieldUserValue PersonField { get; set; }

        [SPFieldFlag]

        public SPFieldUserValueCollection PersonMultiField { get; set; }

        [SPFieldFlag]

        public SPFieldUrlValue HyperLinkField { get; set; }

        [SPFieldFlag]

        public TaxonomyFieldValue MetadataField { get; set; }

        //RestrictUpdate = true, means this  field's value will not be updated.

        [SPFieldFlag(RestrictUpdate = true)]

        public ImageFieldValue PublishingPageImage { get; set; }

        [SPFieldFlag]

        public DateTime? PublishingStartDate { get; set; }

    }

 

SPFieldFlag object shows that property corresponds to a list column. If you want to use a different property name other than column’s internal name, you must pass the internal name of the column to “Name” property of SPFieldFlag attribute.

If you set “RestrictUpdate” property as true, that propy value wont be updated.

 

After created your custom entity, you must create a repository class to get entites. You can see a sample below.

Even you don’t write any methods, you still have methods like GetItemById, AddUpdateItem, GetItems etc.

 

public class AllFieldsListRepository : RepositoryBase<AllFieldsListEntity>

    {

        protected override string ListUrl

        {

            get { return "Lists/AllFieldsList"; }

        }

 

        public AllFieldsListRepository(SPWeb web) : base(web) { }

 

        public IList<AllFieldsListEntity> GetItemsByCustomQuery()

        {

            SPQuery query = new SPQuery();

            query.RowLimit = 1;

            return base.GetItems(query);

        }

    }

 

RepositoryBase class adds entity’s properties as viewfields to SPQuery object If you dont set any.

 

Getting and Adding Entities

 

using (SPSite site = new SPSite(url))

{

    using (SPWeb web = site.OpenWeb())

    {

       AllFieldsListRepository rep = new AllFieldsListRepository(web);

       IList<AllFieldsListEntity> allitems = rep.GetItems();

       IList<AllFieldsListEntity> queriedItems = rep.GetItemsByCustomQuery();

       AllFieldsListEntity item = rep.GetItemById(2);

 

       //Adding new item

       AllFieldsListEntity newItem = new AllFieldsListEntity();

       newItem.Title = "Test Item 3";

       newItem.PersonField = new SPFieldUserValue(web, 16, string.Empty);

       newItem.LookupField = new SPFieldLookupValue(17, null);

       newItem.PublishingStartDate = DateTime.Now;

       newItem.NumberField = 110;

       newItem.MyChoices = "New Choice";

 

       //Setting taxonomy field is a bit tricky

       TaxonomyField taxonomyField = rep.List.Fields["MetadataField"] as TaxonomyField;

       TaxonomyFieldValue taxonomyFieldValue = new TaxonomyFieldValue(taxonomyField);

       taxonomyFieldValue.Label = "Company";

       taxonomyFieldValue.TermGuid = "eb52767d-8bb2-445d-82dd-0a3235c745ac";

 

       newItem.MetadataField = taxonomyFieldValue;

   

       //you can also add attachments like that

       //newItem.Attachments.Add(new AttachmentEntity() { FileBytes = null, FileName = string.Empty, FileUrl = string.Empty});

 

       rep.AddUpdateItem(newItem);               

 }

}

 

AddUpdate method updates list item if item with id which you provided is exists.

 

Using with Document Library

 

public class DocumentsRepository : RepositoryBase<DocumentEntity>

    {

        protected override string ListUrl

        {

            get { return "Documents"; }

        }

        public DocumentsRepository(SPWeb web) : base(web) { }

    }

 

This DocumentRepository can be used as below.

 

using (SPSite site = new SPSite(url))

{

    using (SPWeb web = site.OpenWeb())

    {

       DocumentsRepository rep = new DocumentsRepository(web);

       IList<DocumentEntity> docs = rep.GetItems();

       //Adding document

       rep.AddDocument("/", "filePath");

       //adding to a specific folder

       SPFolder folder = rep.GetFolder("test/2015");

       rep.AddDocument(folder.Url, "filePath");

     }

}

Last edited Jan 19, 2016 at 2:58 PM by taylancelebioglu, version 11