CSOM创建Content Type并指定GUID

SharePoint 2013 Client Object Mode在创建Content Type时有一个限制,不能给Content Type指定一个GUID,只能由系统随机生成。而在用farm solution部署时,则可以在xml中指定好Content Type的GUID,或者用服务器端对象模型来指定GUID。

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了。代码如下:

private string CreateContentType(ClientContext clientContext, Web web)

        {

string ContentTypeID = "0x010048017A06020440BE8498BB193B944C84";            ContentTypeCollection contentTypeColl = clientContext.Web.ContentTypes;

            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";    

转载于:https://www.cnblogs.com/hearticy/p/3821070.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值