Tuesday, October 28, 2014

Working with SharePoint folder properties using CSOM (Client Side Object model ) in SharePoint 2013


Accessing folder properties are but different from  SharePoint list item access. Following code demonstrates  the basics.

Create SharePoint Folder with Properties

using (var clientContext = ypurMethodToGetSpContectt() )
    var lst = clientContext.Web.Lists.GetByTitle("ListName");
    var folder = lst.RootFolder.Folders.Add("FolderName");
    folder.Properties["property1"] = "sample";
    folder.Properties["property1"] = "sample";

Tuesday, September 30, 2014

How to add/update SharePoint user field (person group column) using ECMA Scripts


SharePoint app model supports enough capability to work with SharePoint from JavaScript.

Working with Person Group column is a common task in SharePoint development. Following illustrates how to  update  a single and multiple values to  SharePoint person group column.

Insert / Update Single Value SharePoint Person Group Column (People Field)

Tip: For single value you should refer the person or group by ID

var  user = new SP.FieldUserValue();
user = 1; // this is the ID for User or Group in SharePoint
listitem.set_item("Column Name", user);

Insert / Update Multi Value SharePoint Person Group Column (People Field)

Tip: For multiple users you need to refer it by login name

var  users =[];
users.push(SP.FieldUserValue.fromUser("dev Members")); // adding group
users.push(SP.FieldUserValue.fromUser("i:0#.f|membership|melick@oapp.onmicrosoft.com")); // adding user
listitem.set_item("Column Name", users);

*listitem  this is the list item you need to manipulate. If you are inserting a item this will be created using ListItemCreationInfo and if you are updating a item this will be a reference to the item you seek

Ref: http://msdn.microsoft.com/en-us/library/office/hh185011(v=office.14).aspx

Tuesday, September 9, 2014

Programmatically Change Default Content Type in SharePoint 2013 List


SharePoint list does not have any direct property or method to make content type default. but it is having content type ordering. thus making content type in first of the list automatically makes that content type default.

following shows a sample code

using (var site = new SPSite("url"))
   using (var web = site.AllWebs["yourweb"])
       var lst = web.Lists["Your List"];
       SPContentTypeCollection currentOrder = lst.ContentTypes;
       // make default content type
       var newOrder = currentOrder.Cast<SPContentType>().Where(ct => ct.Name.Contains("Content Type To Default")).ToList();
       lst.RootFolder.UniqueContentTypeOrder = newOrder;