SharePoint 2013 Client Object Mode在建立Content Type時有一個限制,不能給Content Type指定一個GUID,只能由系統隨機生成。而在用farm solution部署時,則能夠在xml中指定好Content Type的GUID,或者用服務器端對象模型來指定GUID。web
SharePoint 2013 SP1發佈以後,這個問題就迎刃而解了。在新的CSOM對象模型中,能夠在建立Content Type時指定GUID。在o365上申請一個免費的開發網站,而後編寫APP就能夠進行測試了。服務器
SP2013 SP1 CSOM SDK的下載地址:測試
http://www.microsoft.com/en-us/download/details.aspx?id=35585網站
當SP的版本升級到SP1以後,或者在o365網站上,咱們就能夠爲Content Type指定惟一的GUID了。代碼以下:orm
private string CreateContentType(ClientContext clientContext, Web web)xml
{對象
string ContentTypeID = "0x010048017A06020440BE8498BB193B944C84"; ContentTypeCollection contentTypeColl = clientContext.Web.ContentTypes;ip
ContentTypeCreationInformation contentTypeCreation = new ContentTypeCreationInformation();開發
contentTypeCreation.Name = "ContentType Name";部署
contentTypeCreation.Description = "Custom Content Type created by CSOM.";
contentTypeCreation.Group = "Custom";
contentTypeCreation.Id = ContentTypeID;
//Add the new content type to the collection
ContentType ct = contentTypeColl.Add(contentTypeCreation);
clientContext.Load(ct);
clientContext.ExecuteQuery();
return ContentTypeID;
}
注意:在建立Content Type時,不能在代碼中指定它的父Content Type類型。例以下面的代碼是不能正常執行的。
contentTypeCreation. ParentContentType =contentTypes.GetById(「0x0100」);
正確的作法是在指定的GUID中直接包含其父類型的ID,以下面所示:
string ContentTypeID = "0x010048017A06020440BE8498BB193B944C84";