Linking content items with ContentLinksManager

Posted by Community Admin on 04-Aug-2018 22:27

Linking content items with ContentLinksManager

All Replies

Posted by Community Admin on 13-Feb-2012 00:00
Hi,

I've some troubles setting up ContentLinks between a custom module (Group) and an Image. I was able to set up a proper link between them using this code:

var groupLive = manager.GetGroups().Where(x => x.Username == this.emailAddress.Text && x.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live).FirstOrDefault();
                        var imageLive = librariesManager.GetImages().Where(x => x.Title == imageName && x.Status == Telerik.Sitefinity.GenericContent.Model.ContentLifecycleStatus.Live).FirstOrDefault();
 
                        var contentLinkManager = ContentLinksManager.GetManager();
                        var link = contentLinkManager.GetContentLink(groupLive.Id, typeof(Group), "GroupImage");
                        if (link == null)
                            link = contentLinkManager.CreateContentLink("GroupImage", groupLive, imageLive);
                         else
                            link.ChildItemId = image.Id;
                        
 
                        contentLinkManager.SaveChanges();

I don't know if this is the right way, but it seems the only way that is working. I also saw an available override on the Group entity, called 'CreateContentLink', but nothing happens.

Now the case is that I'm having a 'register form'. With this form I register a new 'Group'. Besides all information that can be filled in, I also have the ability to upload a photo (avatar).

I work with the ContentLifeCycle, so after I create a new group, I checkout a temp version, edit the properties, checkin the temp and finally publish the master. You can see in my code that I (again) lookup the group and the image, because my findings are that I need to create a ContentLink based on the 'Live' versions of the content. If I don't do this, I create a link between the 'Master' version, which then don't show up in the rest of my site.

Is there any smart way to add new content items and get the guid ID's of the 'Live' versions, without calling the 'GetGroups' and 'GetImages' and so on?

Hope it is clear what my problem is. It works for now, but it does seem a bit too much code.
Anyone who knows some more on this topic?

Thanks!
Daniel
Posted by Community Admin on 14-Feb-2012 00:00
Hello Daniel,

 How exactly do you publish the created item? If you are using mng.Publish, your Publish method should be returning the live item and after that it would be very easy to get its ID and use it to create the ContentLink.

All the best,
Svetoslav Petsov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Posted by Community Admin on 14-Feb-2012 00:00
Hi Svetoslav,

I use this code to publish an item, since other options didn't work for me. Maybe you can show me the right code to checkout, checkin and publish an item?

// Save changes to the database
                group = manager.Lifecycle.CheckIn(temp) as GroupItem;
                manager.RecompileItemUrls(group);
                manager.SaveChanges();
 
                //Publish the news item.
                var bag = new Dictionary<string, string>();
                bag.Add("ContentType", typeof(GroupItem).FullName);
                WorkflowManager.MessageWorkflow(group.Id, typeof(GroupItem), null, "Publish", false, bag);
 
                manager.Provider.SuppressSecurityChecks = false;

Thanks,
Daniel
Posted by Community Admin on 14-Feb-2012 00:00
So, when I use this manager.LifeCycle.Publish, I get a ILifeCycleDataItem back.
What would be the best way to create a content link between an image and a group based on two ID's?

I tried this code but it gives an error that it cannot find the 'GroupItem' type:
link = contentLinkManager.CreateContentLink("GroupImage",
                        groupId, image.Id, GroupsManager.GetDefaultProviderName(), librariesManager.Provider.Name, typeof(GroupItem).Name, typeof(Telerik.Sitefinity.Libraries.Model.Image).Name);

Thanks,
Daniel
Posted by Community Admin on 16-Feb-2012 00:00
Hi Daniel,

 Here's how you can do the publish:

group.ApprovalWorkflowState = "Published";
var live = manager.Lifecycle.Publish(master);
manager.SaveChanges();
Then you can cast the "live" object, which as you said ILifeCycleDataItem to your module type (group item)
var groupItem = live as GroupItem
Finally, you can pass the ID of the groupItem to the ContentLinkManager and create the content link between it and the other item.

All the best,
Svetoslav Petsov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
This thread is closed