Getting DefaultLocation on Client side

Posted by david.ondracek@bayoo.net on 06-Aug-2019 12:52

Hi,

we're developing a custom widget template for a dynamic module, which uses a webservice (api) to retrieve the items and their data. Everything works well, but getting the url of the items seems to be a bit hard. there's a "documentation" for a location service on client side, but it's more confusing than helpful: 

https://www.progress.com/documentation/sitefinity-cms/for-developers-client-side-api

What we need is to know if there's a built-in service to use on CLIENT side to get the default location of an item like it's returned by GetDefaultUrl extension method on server side.

Thank you

David

All Replies

Posted by jread on 08-Aug-2019 12:27

So not directly out of the box but you can add a custom field to the service use the below code to get the default location of the item.  There are two public methods in the below helper class that can get a content items default location.  Also included how to add a custom field to web services: https://www.progress.com/documentation/sitefinity-cms/for-developers-custom-calculated-properties

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Sitefinity.ContentLocations;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Services;

namespace SitefinityWebApp
{
	public static class ContentLocationHelper
	{
		/// <summary>
		/// Retrives the page url of where the content item exists
		/// </summary>
		/// <param name="content">Content Item</param>
		/// <returns>Url of page where content item is publish. This does not return the conent items default location.</returns>
		public static string GetDefaultPageUrl(IDataItem content)
		{
			string url = string.Empty;
			IContentItemLocation itemLocation = GetItemLocation(content);

			if (itemLocation != null)
			{
				url = PagesHelper.GetPageUrlById(itemLocation.PageId);
			}
			return url;
		}

		/// <summary>
		/// Gets content items default location
		/// </summary>
		/// <param name="content">Conent item</param>
		/// <returns>Content item location object</returns>
		private static IContentItemLocation GetItemLocation(IDataItem content)
		{
			var locationsService = SystemManager.GetContentLocationService();
			var itemLocation = locationsService.GetItemDefaultLocation(content.GetType(),content.Provider.ToString(), content.Id);
			return itemLocation;
		}

		/// <summary>
		/// Get content items full url path
		/// </summary>
		/// <param name="content">Content item</param>
		/// <returns>Full url path to conent item</returns>
		public static string GetContentItemFullUrl(IDataItem content)
		{
			string url = string.Empty;
			IContentItemLocation itemLocation = GetItemLocation(content);

			if (itemLocation != null)
			{
				url = itemLocation.ItemAbsoluteUrl;
			}
			return url;
		}
	}
}


using System;
using System.Collections;
using System.Collections.Generic;
using Telerik.Sitefinity;
using Telerik.Sitefinity.Data;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Web.Services.Extensibility;

namespace SitefinityWebApp
{
    internal class ProviderNameProperty : CalculatedProperty
    {
        public override Type ReturnType
        {
            get
            {
                return typeof(string);
            }
        }

        public override IDictionary<object, object> GetValues(IEnumerable items, IManager manager)
        {
            var ret = new Dictionary<object, object>();
            foreach (IDataItem item in items)
            {
                ret.Add(item, SitefinityWebApp.GetDefaultPageUrl(item)); } 
return ret;
}
}
}


This thread is closed