2011-07-21 79 views
6

var objWeb = properties.Feature.Parent as SPWeb;sharepoint:将现有网站列添加到现有内容类型编程方式

SPContentType contentType = objWeb.ContentTypes["Wiki Page"]; 
if (!contentType.Fields.ContainsField("Keywords")) 
{ 
    SPField field = objWeb.Fields["Keywords"]; 
    SPFieldLink fieldLink = new SPFieldLink(field); 
    contentType.FieldLinks.Add(fieldLink); 
    contentType.Update(true); 
} 

我使用这个代码在功能激活网站栏“关键字”添加到网站内容类型“Wiki页面”我的问题是“关键词”,从现有的网站列是添加添加在“维基页面”,而不是新网站专栏。我的代码有问题吗?

一两件事,当我office365这个问题,我发现

部署在此代码的工作我的MOSS服务器上的微细
+0

我有一个类似的代码解决方案,它在SP2010中工作。但是,这是一个“农场”解决方案。 Office365只接受沙盒解决方案。 –

+0

您是使用web.AvailableContentTypes还是web.ContentTypes创建此对象? –

回答

12

你应该试试下面的代码:

if (objWeb.IsRootWeb) 
{ 
    SPContentType contentType = objWeb.ContentTypes["Wiki Page"]; 
    if (!contentType.Fields.ContainsField("Keywords")) 
    { 
     SPField field = objWeb.Fields["Keywords"]; 
     SPFieldLink fieldLink = new SPFieldLink(field); 
     contentType.FieldLinks.Add(fieldLink); 
     contentType.Update(true); 
    } 
} 
else 
{ 
    SPContentType contentTyperoot = site.RootWeb.ContentTypes["Wiki Page"]; 
    if (!contentTyperoot.Fields.ContainsField("Keywords")) 
    { 
    SPContentType contentType = site.RootWeb.ContentTypes["Wiki Page"]; 
    if (!contentType.Fields.ContainsField("Keywords")) 
    { 
     SPField field = site.RootWeb.Fields["Keywords"]; 
     SPFieldLink fieldLink = new SPFieldLink(field); 
     contentType.FieldLinks.Add(fieldLink); 
     contentType.Update(true); 
    } 
    } 
} 

我希望有人正在从帮助我代码:)

相关问题