SharePointAds TextOnly

Showing posts with label SharePoint Client Object Model. Show all posts
Showing posts with label SharePoint Client Object Model. Show all posts

Thursday, 16 May 2013

SharePoint Client Object Model : How to change default content type programatically

In this blog I am going to show how to change default content type programatically using the SharePoint client object model. In one of my task assignments I was using custom content type in the document library and to update this new content type as a default content type for the document library.
The top content type is always default content type for all type of SharePoint libraries. So that you have to just change the content type order of library and make your custom content type on the top.
Following is the code method to change the content type order.
Include "using SP = Microsoft.SharePoint.Client;" on the top of your class.

void ChangeContentTypeOrder(SP.ClientContext ctx, SP.List list, string contentTypeName)
{
            SP.ContentTypeCollection currentCTOrder = list.ContentTypes;
            ctx.Load(currentCTOrder);
            ctx.ExecuteQuery();

            IList<SP.ContentTypeId> reverceOrder = new List<SP.ContentTypeId>();
            foreach (SP.ContentType ct in currentCTOrder)
            {
                if (ct.Name.Equals("contentTypeName"))
                {
                    reverceOrder.Add(ct.Id);
                }
            }
            list.RootFolder.UniqueContentTypeOrder = reverceOrder;
            list.RootFolder.Update();
            list.Update();
            ctx.ExecuteQuery();           
}



Where "SP.List list" is the list name and  'string contentTypeName' is the custom content type which you have to make as a default content type

Tuesday, 3 January 2012

SharePoint Client Object Model: CAML query execution using pagination

Below approach is used when user wants to perform pagination based on given parameters. In order to make paging work, you need to get page index and recurring count values from user.
List Items can be retrive in result and user can specify PageIndex and RecCount values which will be used for resulting certain number of values.


If your list is having more than 1 million and user requested only for list items in the rage of 100-200 then it is not good practice to get all list item collection. To avoid to load all list items collection you need to get required list item from CAML and using Paging. The logic is :

  1. Calculate skip value
  2. Assign skip values to ListItemCollectionPosition PagingInfo e.g. Paged=TRUE&p_ID=SKIP_NUMBER”
  3. Pass ListItemCollectionPosition value to CAML query
  4. Apply RowLimit value to CAML query

Suppose if you are trying to get list items from 100 to 200 then assign PageIndex =2 and recCount =100 into the following method.
//Add client
using SP = Microsoft.SharePoint.Client;

public void getItems(int pageIndex, int recCount)
{
SP.ClientContext ctx = getClientContext(); //authenticate client context
                                          // first Authentication post for more details

if (ctx != null)
{
              int iSkip = pageIndex * recCount - recCount;
              SP.List list = ctx.Web.Lists.GetByTitle("Your LIst Name"); // replace with your list name
              ListItemCollectionPosition itemPosition = new ListItemCollectionPosition();
              itemPosition.PagingInfo = string.Format("Paged=TRUE&p_ID={0}",iSkip);
              CamlQuery camlQuery = new CamlQuery();
              camlQuery.ListItemCollectionPosition = itemPosition;

              camlQuery.ViewXml = @"<View><ViewFields>
                                    <FieldRef Name='ID'/>
                                    <FieldRef Name='Title'/>
                                    </ViewFields><RowLimit>" + recCount + @"</RowLimit>
                                    </View>";
                  
SP.ListItemCollection listItems = list.GetItems(camlQuery);
              ctx.Load(listItems);
              ctx.ExecuteQuery();

              foreach (SP.ListItem listItem in listItems)
              {
//Do forward with listItem["ID"].ToString() and  listItem["Name"].ToString()
              }
}
}


Please have a look at Authentication post for more details about SharePoint Client object model authentication.