List Aggregator App (SPFx)

How to retrieve site url based on site title using .Net managed object model code in SharePoint online?

SharePoint online retrieve site url based on site title

In this post we will discuss how we can retrieve site url based on site name using .Net managed object model (csom) code in SharePoint online Office 365. Here we will work in a console application and we will try to connect to SharePoint online (Download PDF for FREE) site.

Here in this example, we are checking with one title as “Bhawana” and if the site title is “Bhawana” then it will return the URL. In the method we are loading the Title and URL property of the web.

SharePoint deveopment training course

Below is the full code:




private static string GetSiteURLFromSiteTitle()
{
string siteURL = string.Empty;
string siteTitle = “Bhawana”;
using (ClientContext clientContext = new ClientContext(“https://onlysharepoint2013.sharepoint.com/sites/Bhawana/”))
{
clientContext.AuthenticationMode = ClientAuthenticationMode.Default;
clientContext.Credentials = new SharePointOnlineCredentials(GetSPOAccountName(), GetSPOSecureStringPassword());
Web web = clientContext.Web;
clientContext.Load(web, w => w.Title, w => w.Url);
clientContext.ExecuteQuery();
if (web.Title == siteTitle)
{
siteURL = web.Url;
}
}
return siteURL;
}

private static SecureString GetSPOSecureStringPassword()
{
try
{
var secureString = new SecureString();
foreach (char c in ConfigurationManager.AppSettings[“SPOPassword”])
{
secureString.AppendChar(c);
}
return secureString;
}
catch
{
throw;
}
}

private static string GetSPOAccountName()
{
try
{
return ConfigurationManager.AppSettings[“SPOAccount”];
}

catch
{
throw;
}
}

Read some sharepoint online tutorials:


Once you run the code, you can see it will return the url like below:

SharePoint online retrieve site url based on site title
SharePoint online retrieve site url based on site title

Hope this will be helpful.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bhawana Rathore

Hello Everyone!! I am Bhawana a SharePoint MVP and having about 8+ years of SharePoint experience as well as in .Net technologies . I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools . Now exploring more in SharePoint 2016 :) Hope here I can contribute and share my knowledge to the fullest. As I believe "There is no wealth like knowledge, and no poverty like ignorance"

View all posts by Bhawana Rathore →