Get the default location of an item

You can get an item's default location by using the overloads of GetItemDefaultLocation method in one of the following ways.

PREREQUISITES: The default location of an item must be a page that is accessible by all users, that is, they have permissions to view it.

Get the default location of an item by its details 

To do this, you use the GetItemDefaultLocation(Type itemType, string itemProvider, Guid itemId, CultureInfo culture = null) overload.
This overload gets the default (canonical) location by specifying the item type, the item’s provider, and the item ID. This method is useful when you have no reference to the actual content item. It returns an IContentItemLocation item that contains the following information:

  • The absolute URL of the item
  • The PageId where the item can be opened
  • The SiteId, which is the ID of the site which the page belongs to.
  • The IsDefault Boolean flag that is set to true, indicating that this a default location. 

If there is no location where an item can be opened, the method returns null.

The following code sample gets the default location of a news item:

using System;
using Telerik.Sitefinity.News.Model;
using Telerik.Sitefinity.Services;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.HowTo.ContentLocationsAPI
{
public partial class ContentLocationsAPI
{
public static string GetItemDefaultLocationById(Guid itemId, string itemProvider = null)
{
//first we need to get an instance of the location service
var clService = SystemManager.GetContentLocationService();
//gets the item default location of a given item by itemId provided
var location = clService.GetItemDefaultLocation(
typeof(NewsItem),
itemProvider,
itemId);
//gets the absolute url for the location
if (location != null)
return location.ItemAbsoluteUrl;
return string.Empty;
}
}
}

Get the default location of an item by a reference

To do this, you use the GetItemDefaultLocation(IDataItem item, CultureInfo culture = null) overload.

This overload gets the default (canonical) location of type IContentItemLocation, where the specified IDataItem can be opened. This method can be used when there is a reference to the actual content item. It returns an IContentItemLocation item or null if there is no location for item of that type, provider, and language (for multilingual).

The following code sample gets the default location of a news item:

using Telerik.Sitefinity.News.Model;
using Telerik.Sitefinity.Services;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.HowTo.ContentLocationsAPI
{
public partial class ContentLocationsAPI
{
public static string GetItemDefaultLocation(NewsItem item)
{
//first we need to get an instance of the location service
var clService = SystemManager.GetContentLocationService();
//gets the item default location of a given news item
var location = clService.GetItemDefaultLocation(item);
//gets the absolute url for the location
if (location != null)
return location.ItemAbsoluteUrl;
return string.Empty;
}
}
}

Want to learn more?

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?