Get all locations of an item

ContentLocationService API registers all locations of an item in the database. You can access them by calling the GetItemLocations() method of the ContentLocationService. It has two overloads that you can use depending on your use case scenario.

Get an item's location by its details 

You can get the item locations when you do not have the data item, but you know its details - ID, Type, ProviderName
In this case, you can use the overload of the GetItemLocations() method that takes as parameters the content item’s Type , ProviderName , ID , and Culture (for multilingual) and returns an enumeration of IContentItemLocation objects. These are all the locations where the specified item can be opened. If there is no location, the GetItemLocations() method returns an empty collection.

The following code sample gets all locations of a news item by its ID:

C#
using System.Collections.Generic;
using Telerik.Sitefinity.Modules.News;
using Telerik.Sitefinity.News.Model;
using Telerik.Sitefinity.Services;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.HowTo.ContentLocationsAPI
{
   public partial class ContentLocationsAPI
   {
       public static List<string> GetNewsItemLocationsById()
       {
           List<string> result = new List<string>();
           string newsName = "test-news";
           var contentType = typeof(NewsItem);
           var newsManager = NewsManager.GetManager();
           string newsProviderName = newsManager.Provider.Name;

           //Create a news item (no location yet).
           var newsItem = CreateNews(newsManager);

           //gets the item locations using an instance from locationsService
           var locationsService = SystemManager.GetContentLocationService();
           var newsLocations = locationsService.GetItemLocations(contentType, newsManager.Provider.Name, newsItem.Id);

           foreach (var location in newsLocations)
           {
               //get all URLs of the item (even the default one)      
               result.Add(location.ItemAbsoluteUrl);
           }
           return result;
       }
   }
}

Get an item's locations by a reference

You can get the item locations when you have a reference to the item. In this case, you use the GetItemLocations(IDataItem item, CultureInfo culture = null) overload that takes as parameter the actual IDataItem and as optional parameter - the desired culture. It returns an enumeration of IContentItemLocation objects. If there are no locations, it returns an empty collection.

The following code sample gets all locations where a news item is published:

C#
using System.Collections.Generic;
using Telerik.Sitefinity.Modules.News;
using Telerik.Sitefinity.News.Model;
using Telerik.Sitefinity.Services;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.HowTo.ContentLocationsAPI
{
   public partial class ContentLocationsAPI
   {
       public static List<string> GetNewsItemLocations()
       {
           List<string> result = new List<string>();
           string newsName = "test-news";
           var contentType = typeof(NewsItem);
           var newsManager = NewsManager.GetManager();
           string newsProviderName = newsManager.Provider.Name;

           //Create a news item (no location yet).
           var newsItem = CreateNews(newsManager);

           //gets the item locations using an instance from locationsService
           var locationsService = SystemManager.GetContentLocationService();
           var allLocations = locationsService.GetItemLocations(newsItem);

           foreach (var location in allLocations)
           {
               //get all URLs of the item (even the default one)
               result.Add(location.ItemAbsoluteUrl);
           }
           return result;
       }

       public static NewsItem CreateNews(NewsManager manager)
       {
           var newsItem = manager.CreateNewsItem();
           string title = "news-item";
           newsItem.Title.Value = title;
           newsItem.UrlName.Value = title;
           manager.RecompileItemUrls(newsItem);
           manager.Lifecycle.Publish(newsItem);
           manager.SaveChanges();
           return newsItem;
       }
   }
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.