My new blog present here.

Featured Post

Insights into Sitecore Search: A Definitive Introduction

A key component of digital experience management is effective information retrieval. A simplified approach is required for websites, applications, and platforms containing a lot of material so that consumers can easily get the data they require. This need is met by Sitecore, a well-known name in the field of digital experience platforms (DXPs), which provides powerful and comprehensive search functionality. We will travel into the realm of Sitecore Search in this article, learning about its capabilities, architecture , and the enormous value it offers both developers and end users. Introduction to Sitecore Search    A headless content discovery platform powered by AI , Sitecore Search enables you to build predictive and custom search experiences across various content sources. To extract and ...

How to convert absolute url to relative url

How to convert absolute url to relative url:


URL: URL is just a fancy name for "address". You use a "url" to give the address to a link or image.

Absolute url: An url that is ABSOLUTE shows the complete address. In other words, there is no confusion about where this item is located, as the ABSOLUTE URL gives the entire path to that file. http://amitkumarmca04.blogspot.com/2011/02/get-list-data-using-sharepoint-object.html is an example of an ABSOLUTE url.

Relative url: A url that is RELATIVE only shows a partial address - like /2011/02/get-list-data-using-sharepoint-object.html - and the success or failure of finding the file is contingent on certain conditions being met - which means the outcome can and will vary, depending largely how the directories within your website are structured.

Problem:
After restoring the site from development environment to staging environment.
If some document library having metadata like “ListURL”. This metadata contains url in the form of absolute url, then it will show development environment url into staging site. Now, application team decided to change this metadata value from absolute url to relative url, in that case we need to change value, which exist at present in the form of absolute url into relative url.

Resolution:
In this scenario, we can use SharePoint Object Model classes to access data from custom SharePoint list and pass the value of “ListURL” to following function:

public string GetRelativeURL(string urlToConvert)
{
char[] chrsplitter1 = { '/' };
string[] strsplitter1 = { "://" };
string convertedURL = string.Empty;
if (!string.IsNullOrEmpty(urlToConvert) && !string.IsNullOrEmpty(urlToConvert))
{
//If url is not null
//Replace "https" with "http"
if (urlToConvert.ToLower().Contains("https"))
{
urlToConvert = urlToConvert.Replace("https", "http");
}

//Remove "/" from start and end of the string
urlToConvert = urlToConvert.TrimStart(chrsplitter1).TrimEnd(chrsplitter1);

if (urlToConvert.ToLower().Contains("http"))
{
// urlToConvert is a full URL, i.e. absolute URL
// For Example : -- "http://amitkumarmca04.blogspot.com/2011/02/get-list-data-using-sharepoint-object.html"
// So we are converting it into :-- /2011/02/get-list-data-using-sharepoint-object.html

//Replace "\\" with "//"
if (urlToConvert.ToLower().Contains("\\"))
{
urlToConvert = urlToConvert.Replace("\\", "//");
}

//Spilt url with "://"
// For Example : -- "http://amitkumarmca04.blogspot.com/2011/02/get-list-data-using-sharepoint-object.html"
string[] arrHTTP = urlToConvert.Split(strsplitter1, System.StringSplitOptions.RemoveEmptyEntries);
//arrHTTP[0]="http"
//arrHTTP[1]="amitkumarmca04.blogspot.com/2011/02/get-list-data-using-sharepoint-object.html"
if (arrHTTP != null && arrHTTP.Length >= 2)
{
if (!string.IsNullOrEmpty(arrHTTP[1]))
{
//If arrHTTP[1] is not null
string relativeURL = string.Empty;
if (arrHTTP[1].IndexOf("/") >= 0)
{
//If "/" found in the string
relativeURL = arrHTTP[1].Substring(arrHTTP[1].IndexOf("/"));
}
else
{
//If "/" not found in the string
relativeURL = arrHTTP[1];
}

if (!string.IsNullOrEmpty(relativeURL))
{
//We will receive relativeURL ="2011/02/get-list-data-using-sharepoint-object.html"
relativeURL = relativeURL.TrimStart(chrsplitter1).TrimEnd(chrsplitter1);
//We will receive convertedURL ="/2011/02/get-list-data-using-sharepoint-object.html"
convertedURL = "/" + relativeURL;
}
}
}

}
else
{
//We will receive convertedURL ="/2011/02/get-list-data-using-sharepoint-object.html"
convertedURL = "/" + urlToConvert;
}
}
else
{
//If url is null
}
return convertedURL;
}



Above function return the relative url.

Reference: gallery.menalto.com

Comments

Manjeet Singh said…
Good Example .. Thanks

Popular posts from this blog

Sitecore GraphQL Queries

Sitecore Experience Manager Cloud (XM Cloud) Building blocks

Configuring Sitecore Next.js Headless SXA Multisite App in a Sitecore Container