网站内容更新方案( 创建新的网站栏添加到新内容类型和做法项目)

优采云 发布时间: 2021-12-21 23:21

  网站内容更新方案(

创建新的网站栏添加到新内容类型和做法项目)

  替换现场解决方案中的SharePoint 内容类型和网站 列

  此页面有用吗?

  谢谢。

  本文内容

  本文介绍了替换内容类型和网站列时的转换,将网站列添加到新的内容类型,然后使用SharePoint客户端对象模型(CSOM)替换之前的内容类型使用新的内容类型流程。

  重要的

  您不能将场解决方案迁移到 SharePoint Online。通过应用本文中描述的技术和代码,您可以构建一个新的解决方案,该解决方案使用更新的内容类型和 网站 列并将其提交给您的场解决方案或声明性沙箱解决方案提供类似的功能。然后,可以将此新解决方案部署到 SharePoint Online。

  如果要采用本文中的代码,则需要使用其他代码来生成功能齐全的解决方案。例如,本文不讨论如何向 Office 365 进行身份验证、如何实现必要的异常处理等。有关其他代码示例,请参阅 Office 365 开发人员模式和实践项目。

  评论

  本文中的代码按原样提供,不提供任何明示或暗示的保证,包括对特定用途的适用性、适销性或不侵权的暗示保证。

  要使用 CSOM 替换内容类型和 网站 列:

  创建新的内容类型。

  创建一个新的 网站 列(也称为字段)。

  将新的 网站 列添加到新的内容类型。

  用新的内容类型替换旧的内容类型引用。

  在下面的代码中,Main 显示了用 CSOM 和 网站 列替换内容类型所需的操作序列。

  static void Main(string[] args)

{

using (var clientContext = new ClientContext("http://contoso.sharepoint.com"))

{

Web web = clientContext.Web;

CreateContentType(clientContext, web);

CreateSiteColumn(clientContext, web);

AddSiteColumnToContentType(clientContext, web);

// Replace the old content type with the new content type.

ReplaceContentType(clientContext, web);

}

}

  在下面的代码中,GetContentTypeByName通过以下方式从当前网站中获取内容类型:

  使用 Web.ContentTypes 属性获取 ContentType采集,它是 网站 上的当前内容类型集合。

  通过将 网站 内容类型名称与 name 参数提交的现有内容类型名称进行匹配,从 网站 中查找并返回内容类型。

   private static ContentType GetContentTypeByName(ClientContext cc, Web web, string name)

{

ContentTypeCollection contentTypes = web.ContentTypes;

cc.Load(contentTypes);

cc.ExecuteQuery();

return contentTypes.FirstOrDefault(o => o.Name == name);

}

  创建新的内容类型

  在以下代码中, CreateContentType 通过以下方式创建新的内容类型:

  创建一个名为 contentTypeName 的常量来存储内容类型的名称。新内容类型的名称将设置为先前内容类型的名称。

  调用 GetContentTypeByName 以在 网站 上查找匹配的内容类型。

  如果内容类型已存在,则无需进一步操作。当 return 被调用时,控制将被传递回 Main。

  如果内容类型不存在,将使用名为 newCt 的 ContentTypeCreationInformation 对象设置内容类型属性。

  新的内容类型 ID 将使用基本文档内容类型 ID 0x0101 分配给 newCt.Id。有关更多信息,请参阅基本内容类型层次结构。

  使用 ContentType采集.Add 添加新的内容类型。

  private static void CreateContentType(ClientContext cc, Web web)

{

// The new content type will be created using this name.

const string contentTypeName = "ContosoDocumentByCSOM";

// Determine whether the content type already exists.

var contentType = GetContentTypeByName(cc, web, contentTypeName);

// The content type exists already. No further action required.

if (contentType != null) return;

// Create the content type using the ContentTypeInformation object.

ContentTypeCreationInformation newCt = new ContentTypeCreationInformation();

newCt.Name = "ContosoDocumentByCSOM";

// Create the new content type based on the out-of-the-box document (0x0101) and assign the ID to the new content type.

newCt.Id = "0x0101009189AB5D3D2647B580F011DA2F356FB2";

// Assign the content type to a specific content type group.

newCt.Group = "Contoso Content Types";

ContentType myContentType = web.ContentTypes.Add(newCt);

cc.ExecuteQuery();

}

  创建一个新的 网站 列

  在以下代码中,CreateSiteColumn 通过以下方式创建一个新的 网站 列:

  创建一个名为 fieldName 的常量来存储字段的名称。新字段的名称将设置为前一个字段的名称。

  使用 Web.Fields 属性获取 网站 上定义的 网站 列。

  通过将 网站 上的字段名称与 fieldName 匹配来查找 网站 上的匹配字段。如果该字段已存在,则无需进一步操作。当 return 被调用时,控制将被传递回 Main。如果该字段不存在,则将指定字段结构的 CAML 字符串分配给 FieldAsXML,然后将使用 Field采集.AddFieldAsXml 来创建该字段。

  private static void CreateSiteColumn(ClientContext cc, Web web)

{

// The new field will be created using this name.

const string fieldName = "ContosoStringCSOM";

// Load the list of fields on the site.

FieldCollection fields = web.Fields;

cc.Load(fields);

cc.ExecuteQuery();

// Check fields on the site for a match.

var fieldExists = fields.Any(f => f.InternalName == fieldName);

// The field exists already. No further action required.

if (fieldExists) return;

// Field does not exist, so create the new field.

string FieldAsXML = @"";

Field fld = fields.AddFieldAsXml(FieldAsXML, true, AddFieldOptions.DefaultValue);

cc.ExecuteQuery();

}

  将新的 网站 列添加到新的内容类型

  在以下代码中,AddSiteColumnToContentType 通过以下方式在内容类型和字段之间创建关联:

  使用 ContentType.FieldLinks 属性加载内容类型,然后加载该内容类型中的字段引用。

  加载字段。

  使用 contentType.FieldLinks.Any(f => f.Name == fieldName) 匹配字段名来判断内容类型是否引用该字段。

  如果内容类型已引用该字段,则无需进一步操作。当 return 被调用时,控制将被传递回 Main。如果内容类型不引用字段,则在 FieldLinkCreationInformation 对象上设置字段引用属性。

  将 FieldLinkCreationInformation 对象添加到 ContentType.FieldLinks 属性。

  private static void AddSiteColumnToContentType(ClientContext cc, Web web)

{

// The name of the content type.

const string contentTypeName = "ContosoDocumentByCSOM";

// The field name.

const string fieldName = "ContosoStringCSOM";

// Load the content type.

var contentType = GetContentTypeByName(cc, web, contentTypeName);

if (contentType == null) return; // content type was not found

// Load field references in the content type.

cc.Load(contentType.FieldLinks);

cc.ExecuteQuery();

// Load the new field.

Field fld = web.Fields.GetByInternalNameOrTitle(fieldName);

cc.Load(fld);

cc.ExecuteQuery();

// Determine whether the content type refers to the field.

var hasFieldConnected = contentType.FieldLinks.Any(f => f.Name == fieldName);

// A reference exists already, no further action is required.

if (hasFieldConnected) return;

// The reference does not exist, so we have to create the reference.

FieldLinkCreationInformation link = new FieldLinkCreationInformation();

link.Field = fld;

contentType.FieldLinks.Add(link);

contentType.Update(true);

cc.ExecuteQuery();

}

  用新的内容类型替换旧的内容类型引用

  在以下代码中,ReplaceContentType 检查所有库中的所有项目,查找引用旧内容类型的内容,然后将这些引用替换为新内容类型 (ContosoDocumentByCSOM):

  将旧的内容类型 ID 分配给一个常量。

  使用 GetContentTypeByName 获取新的内容类型。

  使用 Web.Lists 获取 网站 上的所有列表。

  使用 cc.Load(lists, l => l.Include(list => list.ContentTypes) 加载 网站 上的所有列表以及每个列表的所有内容类型。

  对于每个返回的列表,使用 list.ContentTypes.Any(c => c.StringId.StartsWith(oldContentTypeId)) 搜索列表中的内容类型并将内容类型与旧的内容类型 ID 进行匹配。如果找到匹配项,则收录旧内容类型的列表将添加到listsWithContentType。

  对于listsWithContentType 中的每个列表:

  确定新的内容类型是否已附加到列表中。如果新内容类型未附加到列表中,请使用 ContentType采集.AddExistingContentType 将新内容类型附加到列表中。

  获取列表中的所有列表项。

  对于每个列表项,获取列表项的内容类型 ID。确定列表项的内容类型 ID 是否与旧的内容类型 ID 相同。如果它们不同,请跳到下一个列表项。如果它们相同,请使用 ContentType.StringId 为列表项分配新的内容类型 ID。

  评论

  列表中仍有旧的内容类型,但这些类型不再可用。现在可以从列表中删除和撤回旧的内容类型。本文介绍了如何仅替换文档内容类型。要替换页面布局上的内容类型,请确保更新 网站 集中每个页面布局的 AssociatedContentType 属性。

  private static void ReplaceContentType(ClientContext cc, Web web)

{

// The old content type.

const string oldContentTypeId = "0x010100C32DDAB6381C44868DCD5ADC4A5307D6";

// The new content type name.

const string newContentTypeName = "ContosoDocumentByCSOM";

// Get the new content type and lists on the site.

ContentType newContentType = GetContentTypeByName(cc, web, newContentTypeName);

ListCollection lists = web.Lists;

// Load the new content type and the content types on all lists on the site.

cc.Load(newContentType);

cc.Load(lists,

l => l.Include(list => list.ContentTypes));

cc.ExecuteQuery();

var listsWithContentType = new List();

foreach (List list in lists)

{

bool hasOldContentType = list.ContentTypes.Any(c => c.StringId.StartsWith(oldContentTypeId));

if (hasOldContentType)

{

listsWithContentType.Add(list);

}

}

foreach (List list in listsWithContentType)

{

// Determine whether the new content type is already attached to the list.

var listHasContentTypeAttached = list.ContentTypes.Any(c => c.Name == newContentTypeName);

if (!listHasContentTypeAttached)

{

// Attach content type to list.

list.ContentTypes.AddExistingContentType(newContentType);

cc.ExecuteQuery();

}

// Get all list items.

CamlQuery query = CamlQuery.CreateAllItemsQuery();

ListItemCollection items = list.GetItems(query);

cc.Load(items);

cc.ExecuteQuery();

// For each list item, determine whether the old content type is used, and then update to the new content type.

foreach (ListItem listItem in items)

{

// Get the current content type for this list item.

var currentContentTypeId = listItem["ContentTypeId"] + "";

var isOldContentTypeAssigned = currentContentTypeId.StartsWith(oldContentTypeId);

// This item does not use the old content type - skip to next list item.

if (!isOldContentTypeAssigned) continue;

// Update the list item content type to the new content type.

listItem["ContentTypeId"] = newContentType.StringId; // new content type Id;

listItem.Update();

}

// Save all changes.

cc.ExecuteQuery();

}

}

  也可以看看

  主题

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线