How to add contenttype to a list by web services?
- First I want to describe my goal:
I want to add a new task to a tasklist from an external program by the Lists web service. The edit and display forms of this task should be custom forms outside of sharepoint.
Here my approach:
I think the first step is to create a new contenttype. Here is my code:
Dim xmlFields As XmlNode = xmlDoc.CreateNode(XmlNodeType.Element, "FieldRefs", "")
Dim xmlProps As XmlNode = xmlDoc.CreateNode(XmlNodeType.Element, "ContentType", "")
Lists.CreateContenttYpe("MyTaskList", "MyContentType", "0x010800B5D5C4D05B34F243873A78D33F5F5CDB", xmlFields, xmlProps, "true")
The result is a new contenttype that contains the title field only. The contenttype id begins with 0x0100 and not with 0x010800.
Why it doesn't inherits the fields of the contenttype "Task"?
Why it isn't of type "Task"?
Then I tried to add a standard field programmatically:
Dim xmlFieldStatus As XmlElement = xmlDoc.CreateElement("FieldRef")
xmlFieldStatus.SetAttribute("ID", "c15b34c3-ce7d-490a-b133-3f4de8801b76")
xmlFieldStatus.SetAttribute("Name", "Status")
xmlFields.AppendChild(xmlFieldStatus)
But nothing happens. The new contenttype still contains only the title field.
How can I add standard available fields to my new contenttype?
How to add the FormUrls for my custom forms to my contenttype?
Answers
More info. Could only delete inherited columns by updating the content type after creating it.
So I called the CreateContentType() web method as above.
Then I called UpdateContentType() to delete any inherited columns that should be removed.
Here's the code, which calls some custom private methods that retrieve site column and content type IDs.
The custom methods are not fully listed here for clarity.
// Get list of all Content Type XML spec files.fileList =
Directory.GetFiles(Directory.GetCurrentDirectory(), "ContentType*.xml"); foreach (string fullFilename in fileList){
// Set up Delete Fields XML fragment. XmlElement DeleteFields = xmlDoc.CreateElement("Fields"); // Set up empty New Fields and Update Fields XML fragments. XmlElement NewFields = xmlDoc.CreateElement("Fields"); XmlElement UpdateFields = xmlDoc.CreateElement("Fields");xr =
XmlReader.Create(fullFilename);xr.MoveToContent();
while (xr.Read()){
if (xr.NodeType != XmlNodeType.EndElement){
if (xr.Name == "ContentType"){
if (xr.MoveToAttribute("Name")){
strContentTypeName = xr.Value.ToString();
strSPContentTypeID = GetSPContentTypeID(strContentTypeName);
}
}
if (xr.Name == "RemoveFieldRef"){
if (xr.MoveToAttribute("Name")){
strFieldRefName = xr.Value.ToString();
}
strSPSiteColumnID = GetSPSiteColumnID(strFieldRefName);
sb.Append(
"<Method ID='3'><Field Name='" + strFieldRefName + "' ID='" + strSPSiteColumnID + "'/></Method>");}
}
}
DeleteFields.InnerXml = sb.ToString();
SetCredentials(
ref WebsWebService); XmlNode returnValue = WebsWebService.UpdateContentType(strSPContentTypeID, xmlProps, NewFields, UpdateFields, DeleteFields);}
// end for each fullFilename ...
All Replies
I've met the same problem, please tell me once you resolve it. Thanks a lot.
you can contact me with chje_0114@yahoo.com.cnI resolved the contenttype FieldRefs issue by sending in fields in this XML fragment format for xmlFields.
<Fields>
<Method ID='1'>
< Field Name='[ContentTypeName]>' ID='[SiteColumnID]'>
</Method>
<Method ID='1'>
<Field Name='[ContentTypeName]' ID='[SiteColumnID]'>
</Method>
</Fields>
I had to create an XML element with XmlDocument.CreateElement("Fields") and then add the rest of the XML with the InnerXML property.
XmlElement newFields = xDoc.CreateElement("Fields");
newFields.InnerXml = "<Method ID='1'><Field Name='FieldName' ID='[xxx Long GUID xxx]'/></Method>";
Adding nodes programmatically for some reason wouldn't work; only using the InnerXML property worked for me.
If you want to send in field references to be deleted, wrap the field descriptions with "Method = '3'."
Method='2' is for updating fields, at least for site columns.
This is different from the Microsoft online and SDK documentation, but I did find some preliminary Microsoft documents released this very month that delineated this format as the SOAP IN protocol.
I went through a lot of stress with this issue too. I first went through this very same challenge when creating the site columns that I wanted to reference in my content types. I finally found one online reference where another developer had sweated through this and used the above protocol to create SharePoint site columns. Then when I started going through similar troubles with CreateContentType(), I tried the same protocol and thankfully it worked!
More info. Could only delete inherited columns by updating the content type after creating it.
So I called the CreateContentType() web method as above.
Then I called UpdateContentType() to delete any inherited columns that should be removed.
Here's the code, which calls some custom private methods that retrieve site column and content type IDs.
The custom methods are not fully listed here for clarity.
// Get list of all Content Type XML spec files.fileList =
Directory.GetFiles(Directory.GetCurrentDirectory(), "ContentType*.xml"); foreach (string fullFilename in fileList){
// Set up Delete Fields XML fragment. XmlElement DeleteFields = xmlDoc.CreateElement("Fields"); // Set up empty New Fields and Update Fields XML fragments. XmlElement NewFields = xmlDoc.CreateElement("Fields"); XmlElement UpdateFields = xmlDoc.CreateElement("Fields");xr =
XmlReader.Create(fullFilename);xr.MoveToContent();
while (xr.Read()){
if (xr.NodeType != XmlNodeType.EndElement){
if (xr.Name == "ContentType"){
if (xr.MoveToAttribute("Name")){
strContentTypeName = xr.Value.ToString();
strSPContentTypeID = GetSPContentTypeID(strContentTypeName);
}
}
if (xr.Name == "RemoveFieldRef"){
if (xr.MoveToAttribute("Name")){
strFieldRefName = xr.Value.ToString();
}
strSPSiteColumnID = GetSPSiteColumnID(strFieldRefName);
sb.Append(
"<Method ID='3'><Field Name='" + strFieldRefName + "' ID='" + strSPSiteColumnID + "'/></Method>");}
}
}
DeleteFields.InnerXml = sb.ToString();
SetCredentials(
ref WebsWebService); XmlNode returnValue = WebsWebService.UpdateContentType(strSPContentTypeID, xmlProps, NewFields, UpdateFields, DeleteFields);}
// end for each fullFilename ...


