none
SP Gugu: How to Programmatically change a list item's ContentType by WSS WebService?

    Question

  • Please, help! I can't find any information on MSDN under WSS3 webservices List.UpdateListItems topics

    how to programmatically change a list item ContentTypeId ?

     

    Calling SharePoint list service like: (adding a list item)

     

    XmlElement elBatch = doc.CreateElement("Batch");

    elBatch.SetAttribute("OnError", "Continue");

    elBatch.SetAttribute("ListVersion", "2");

    XmlElement el1 = doc.CreateElement("Method");

    el1.SetAttribute("ID", "1");

    el1.SetAttribute("Cmd", "New");

    XmlElement field1 = doc.CreateElement("Field");

    field1.SetAttribute("Name", "ID");

    field1.InnerText = "New";

    XmlElement field2 = doc.CreateElement("Field");

    field2.SetAttribute("Name", "emp_no");

     XmlNode response = listService.UpdateListItems("Tasks", elBatch);

     

    Wednesday, February 14, 2007 8:56 PM

Answers

  • Hey there,

    I just came across this problem recently and have a solution just in case anyone stumbles upon this thread.

      // Grab the content type from the list so we can get the id
                SPContentType spctExampleContentType = splExampleList.ContentTypes["Example Content Type"];

      // Create the new item
                SPListItem spliNewItem = splExampleList.Items.Add();

      // Now set the content type id
                spliNewItem["ContentTypeId"] = spctExampleContentType.Id;
                spliNewItem.Update();

      // now we have access to set all the content type fields
      spliNewItem["Content Type Field 1"] = "Row";
      spliNewItem["Content Type Field 2"] = "Sham";
      spliNewItem["Content Type Field 3"] = "Bow";

    Pretty self explanatory, I'm not looking at my code right now but that is the general jist of it.

    Hope that helps!!
    Arnie

    Sunday, November 25, 2007 6:35 AM

All replies

  • the desition was found:

    XmlElement field1 = doc.CreateElement("Field");

    field1.SetAttribute("Name", "ContentType");

    field1.InnerText = "theContentTypeName";

    Friday, February 16, 2007 7:36 AM
  • In order to update metadata you need to specify reference to the file that you want to update, here I found answer to my question:

    http://www.codecomments.com/message376403.html

    For me goal was to upload files to Sharepoint Programmatically and update metadata (Indexes, Column information) using Web Services.

    Wednesday, February 21, 2007 5:34 PM
  • What is the corresponding way to set an SPListItem's ContentType directly using the WSS object model?
    Saturday, March 03, 2007 2:33 PM
  • Hey there,

    I just came across this problem recently and have a solution just in case anyone stumbles upon this thread.

      // Grab the content type from the list so we can get the id
                SPContentType spctExampleContentType = splExampleList.ContentTypes["Example Content Type"];

      // Create the new item
                SPListItem spliNewItem = splExampleList.Items.Add();

      // Now set the content type id
                spliNewItem["ContentTypeId"] = spctExampleContentType.Id;
                spliNewItem.Update();

      // now we have access to set all the content type fields
      spliNewItem["Content Type Field 1"] = "Row";
      spliNewItem["Content Type Field 2"] = "Sham";
      spliNewItem["Content Type Field 3"] = "Bow";

    Pretty self explanatory, I'm not looking at my code right now but that is the general jist of it.

    Hope that helps!!
    Arnie

    Sunday, November 25, 2007 6:35 AM
  •  

    To change the content type of an existing list item:

     

      SPContentType myType = myWeb.ContentTypes["myType"];
      SPListItem myListItem = myList.Items[4];
      myListItem["ContentTypeId"] = myType.Id;

    ----------------------

    Or try using the field Guid or display name of the ListItem's content type id field:

     

    SPContentTypeId myCTID = new SPContentTypeId("guid of the target content type");

    myListItem["Content Type ID"] = myCTID;

    // or via guid:

    Guid oG = new Guid("guid of the listitem's content type field");

    myListItem[oG] = myCTID;

    Wednesday, April 16, 2008 4:17 PM
  • I am using the same code. The web service returns no error (return code is something like 0x000000), but the content type is not updated.

    In the same update statement I am also changing other fields, and they are changed correctly.

    What can be wrong?

    Thursday, April 24, 2008 1:25 PM
  • Is there any way to capture a data view forms added event in the aspx page to add your code?
    Saturday, August 09, 2008 7:16 PM