c# - 共享点 : Add existing site column to existing content type Programatically

标签 c# sharepoint-2010

var objWeb = properties.Feature.Parent as SPWeb;

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);
}

我在功能激活中使用此代码将网站栏“KeyWord”添加到网站内容类型“Wiki 页面”我的问题是“关键字”添加到“wiki 页面”但不是从现有网站栏添加新网站栏。我的代码有问题吗?

还有一件事,当我在 office365 上部署时,这段代码在我的 MOSS 服务器上运行良好,我发现了这个问题

最佳答案

你应该试试下面的代码:

 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);
     }
   }
 }

我希望有人能从我的代码中得到帮助:)

关于c# - 共享点 : Add existing site column to existing content type Programatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6772233/

相关文章:

c# - 打开xml查询excel单元格

c# - int 和 null 之间没有隐式转换

c# - SPListItem 更新时保存冲突

发布后,SharePoint Designer工作流不会更改

xslt - 数数XSLT 1.0中的非空节点集

sharepoint-2010 - sharepoint 2010 excel 服务 - 编辑 excel 时面临的问题

c# - XML 到 JSON - 将 Json 对象转换为 Json 数组

c# - 异步/等待执行差异

c# - 使用 EF Core 将表达式树转换为 SQL

jquery - SharePoint 2010 使用 SPServices 获取用户 ID