如何从集合中删除内容并将其添加到另一个集合中
本文关键字:集合 添加 另一个 删除 | 更新日期: 2023-09-27 18:27:06
我用智能表单创建了几个提供程序:
ID Title
90 Doctor A
102 Doctor B
10 Doctor C
26 Doctor D
495 Doctor E
我在CMS中有三个集合:
ID Title of Collection
12 IM Collection
43 UR Collection
9 EC Collection
以下代码检索对我有用的集合的内容:
ContentManager contentManager = new ContentManager();
ContentCollectionCriteria criteria = new ContentCollectionCriteria(ContentProperty.Id, EkEnumeration.OrderByDirection.Ascending);
criteria.AddFilter(Convert.ToInt64(ddlCollection.SelectedValue));
List<ContentData> contentList = contentManager.GetList(criteria);
我将包括以下变量:
iPName (int) = the provider ID
sColl (List<string>) = The collection(s) the provider should go in
如何为每个提供程序编写递归函数,该函数将使用iPName,从任何存在提供程序的集合中删除该提供程序,并使用sCol将该提供程序添加到集合中。
由于您想修改集合,您需要查看集合特定的API管理器类位于Ektron.Cms.Framework.Organization
命名空间中。
基本思想是:
- 获取提供程序和它们所在的每个集合之间的映射
- 给定
iPName
,获取此提供程序当前所在的集合的列表 - 比较#2和
sColl
的列表(例如,取差值) - 任何当前不包含提供程序但应该添加该提供程序的集合
- 任何当前确实包含提供程序但不应该包含的集合,请将其删除
这里有一些粗略的代码可以让你开始(未经测试)
//this is your existing code, wrapped into a function
List<ContentData> GetCollectionContent(long collectionID)
{
var contentManager = new ContentManager();
var criteria = new ContentCollectionCriteria(ContentProperty.Id, EkEnumeration.OrderByDirection.Ascending);
criteria.AddFilter(collectionID);
return contentManager.GetList(criteria);
}
//builds the mapping from step #1 above
Dictionary<ContentData, List<ContentCollectionData>> BuildCollectionMapping()
{
//get all the collections in the system (using a new, "default" criteria object)
var collectionManager = new CollectionManager();
var allCollections = collectionManager.GetList(new CollectionCriteria());
//build the mapping, associate a content item with each collection it is in
var mapping = new Dictionary<ContentData, List<ContentCollectionData>>();
foreach (var collection in allCollections)
{
var contentItems = GetCollectionContent(collection.Id);
foreach (var contentItem in contentItems)
{
if (!mapping.ContainsKey(contentItem))
{
mapping.Add(contentItem, new List<ContentCollectionData>());
}
mapping[contentItem].Add(collection);
}
}
return mapping;
}
//steps #2-3 from above, using the variables you defined
void Reconcile(long iPName, List<string> sColl)
{
var mapping = BuildCollectionMapping();
if (mapping.Keys.Any(content => content.Id == iPName))
{
var collections = mapping.Single(kvp => kvp.Key.Id == iPName).Value;
var collectionTitles = collections.Select(c => c.Title);
//these are the names of collections to which this content item must be added
var toAdd = sColl.Except(collectionTitles);
//these are the names of collections from which the content item must be deleted
var toDelete = collectionTitles.Except(sColl);
}
}
我将由你来填写#4-5的详细信息。
顺便说一句,我想指出的是,Ektron中的ID应该始终表示为long(上面的iPName
示例是int
)。我们刚刚修改了我们的环境,使得现在在64位范围内生成内容ID(例如53687091658
),并且我们遇到了一些对int
的草率解析导致运行时错误的情况。我提供的示例代码使用long。