Pages

Search This Blog

Sunday, February 13, 2011

Advantage of Storing custom information in Content Type XmlDocument Element

Content types enable us to manage the settings for a category of information in a centralized, reusable way. It allows us to store information in a number of ways. One of them which I found very important is XmlDocument. Many times we have project requirements where we are supporting inheritance in custom content types and there comes XmlDocument very handy. XmlDocument elements included in a site content type are automatically copied into any children based on that content type and we are not required to do any custom code for that. A content type can include any number of XMLDocument elements and we can manipulate them programattically through the object model. The contents of each XmlDocument element can conform to any given schema; however, they must be valid XML.

Add XmlDocument to Custom Content type:

public SPContentTypeId CreateContentType(out Boolean IsExists, XmlNode objNode)
{
SPContentTypeId ContentTypeID = default(SPContentTypeId);
SPContentType CustomContentType = null;
SPWeb objSPRootWeb = SPContext.Current.Web;
XmlDocument doc = new XmlDocument();

try
{
CustomContentType = new SPContentType(objSPRootWeb.AvailableContentTypes[“Document”], objSPRootWeb.ContentTypes, “TestContentType”);
CustomContentType.Description = “TestContentType Description”;
CustomContentType.Group = “TestContentType Group”;
doc.LoadXml(objNode.OuterXml);
CustomContentType.XmlDocuments.Add(doc);
objSPRootWeb.ContentTypes.Add(CustomContentType);
CustomContentType.Update();
ContentTypeID = CustomContentType.Id;
}
catch (Exception Ex)
{
}
return ContentTypeID;
}

Retreive a XmlDocument:
CustomContentType.XmlDocuments[index];

Delete a XmlDocument:

CustomContentType.XmlDocuments.Delete(“Xml schema namespace that you provided in xml document”);

Thanks

No comments:

Post a Comment