How to remove content from a collection and add it to another

I have a couple of providers created using smartform:

ID Title 90 Doctor A 102 Doctor B 10 Doctor C 26 Doctor D 495 Doctor E 

I have three collections in CMS:

 ID Title of Collection 12 IM Collection 43 UR Collection 9 EC Collection 

The following code retrieves the contents for a collection that works for me:

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

I will include the following variable:

 iPName (int) = the provider ID sColl (List<string>) = The collection(s) the provider should go in 

How can I encode a recursive function for each provider that takes iPName, remove it from any collection that the provider has, and use sCol to add the provider to the collection.

0
source share
1 answer

Since you want to change collections, you need to look at the API for specific collections. The manager class is inside the Ektron.Cms.Framework.Organization namespace.

Main idea:

  • Get a mapping between suppliers and each collection in which they are located.
  • Provided by iPName , get the list of Collections that this provider is currently located in
  • Compare the C # 2 list on sColl (e.g. take the difference)
  • Any collections that do not currently contain a provider but must add one
  • Any collections that currently contain a provider but should not delete it

Here is an example of rough code to get you started (untested)

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

I will leave this for you to fill out the details # 4-5.

In a small aspect, I want to point out that identifiers in Ektron should always be represented as long (your example above is iPName is int ). We just changed our environment in such a way that content identifiers are now generated in the 64-bit range (for example, 53687091658 ), and we are faced with several cases where inaccurate parsing on an int leads to runtime errors. The sample code I provided uses a long one .

0
source

Source: https://habr.com/ru/post/1433703/


All Articles