locked
Apply a Content Type to an item with the client model RRS feed

  • Question

  • Hi,

    I am trying to apply a content type to a list item with the Microsoft.Sharepoint.Client, but the Contentype property seems to be read only.

    I want to do the same than this but in the client context (notice that this code is in the server model),

    void SetContentType (SPListItem item, SPContentType ct)
    {
      item["ContentTypeId"] = ct.Id;
      item.Update();
    }

    Does anyone how to do it?

    Regards


    Gabriel Renom - Business Systems Analyst - Pannone LLP - Blog: www.gabrielrenom.net
    Wednesday, October 12, 2011 11:13 AM

Answers

  • hi Gabriel,

    The following code worked for me:

     

     using (ClientContext context = new ClientContext("Siteurl"))
                {
    
                    ContentTypeCollection cts = context.Web.AvailableContentTypes;
                    context.Load(cts);
                    context.ExecuteQuery();
    
                    ContentType ct = cts[6];
                    context.Load(ct);
                    context.ExecuteQuery();
    
                    List list = context.Web.Lists.GetByTitle("myDemo");
                    context.Load(list);
                    context.ExecuteQuery();
    
                    ListItem listItem = list.GetItemById(1);
                    context.Load(listItem);
                    context.ExecuteQuery();
    
                    Console.WriteLine("B:"+listItem["ContentTypeId"].ToString());
                    
                    listItem["ContentTypeId"] = ct.Id;
    
                    context.ExecuteQuery();
    
                    listItem.Update();
                    list.Update();
    
                    
                    Console.WriteLine("A:" + listItem["ContentTypeId"].ToString());
    
                    
                }
    
    Make sure you call listItem.Update(); and list.Update() after context.ExecuteQuery();
    Wednesday, October 12, 2011 2:47 PM

All replies

  • Hey,

    string siteUrl = "siteurl";
    ClientContext = new ClientContext(siteUrl);
    SP.List oList = clientContext.Web.Lists.GetByTitle("Test");
    
    ListItem oListItem = oList.GetItemById(6);
    oListItem["ContentTypeID"]=ct.id;
    olistitem.update();
    clientContext.ExecuteQuery(); 
    


    Hope this helps :)


    "The Only Way To Get Smarter Is By Playing A Smarter Opponent"
    Wednesday, October 12, 2011 12:30 PM
  • This code is not working,

    This is what it really works:

    List list = clientContext.Web.Lists.GetByTitle("Documents");

    CamlQuery query = new CamlQuery();

    query.ViewXml = @"
    <View>
    <Query>
    <Where>
    <Eq>
    <FieldRef Name='FileLeafRef'/>
    <Value Type='Text'>myfolder</Value>
    </Eq>
    </Where>
    </Query>
    <RowLimit>10</RowLimit>
    </View>";

    ListItemCollection items = list.GetItems(query);
    clientContext.Load(items);
    clientContext.ExecuteQuery();
    ListItem item4 = items[0];
    item4["ContentTypeId"] = CONTENT TYPE ID
    item4.Update();
    clientContext.ExecuteQuery();

     


    Gabriel Renom - Business Systems Analyst - Pannone LLP - Blog: www.gabrielrenom.net

    Wednesday, October 12, 2011 1:36 PM
  • hi Gabriel,

    The following code worked for me:

     

     using (ClientContext context = new ClientContext("Siteurl"))
                {
    
                    ContentTypeCollection cts = context.Web.AvailableContentTypes;
                    context.Load(cts);
                    context.ExecuteQuery();
    
                    ContentType ct = cts[6];
                    context.Load(ct);
                    context.ExecuteQuery();
    
                    List list = context.Web.Lists.GetByTitle("myDemo");
                    context.Load(list);
                    context.ExecuteQuery();
    
                    ListItem listItem = list.GetItemById(1);
                    context.Load(listItem);
                    context.ExecuteQuery();
    
                    Console.WriteLine("B:"+listItem["ContentTypeId"].ToString());
                    
                    listItem["ContentTypeId"] = ct.Id;
    
                    context.ExecuteQuery();
    
                    listItem.Update();
                    list.Update();
    
                    
                    Console.WriteLine("A:" + listItem["ContentTypeId"].ToString());
    
                    
                }
    
    Make sure you call listItem.Update(); and list.Update() after context.ExecuteQuery();
    Wednesday, October 12, 2011 2:47 PM