Get Locale From SPWeb URL
So I was working today on a some code where I was passing in the URL as a parameter and needed to harvest the locale so that I could get the culture information, the WebPart was going to be involved with a geo-deployment. Here is the code to accomplish that:
[csharp]
public static CultureInfo GetLocale(string url)
{
SPWeb web = GetWeb(url);
CultureInfo locale = web.Locale;
web.Close();
web.Dispose();
return locale;
}
[/csharp]
Notice that you should take into account of disposing of the relevant objects that you are using!
The helper method that you see in the above is just a static wrapper that is called when I want to return a SharePoint Web, as opposed to having to re-code it each time. I keep it in a generic class library.
[csharp]
public static SPWeb GetWeb(string url)
{
SPSite site = new SPSite(url);
return site.OpenWeb();
}
[/csharp]
Happy geo-developing (that should be a word).
Adam, can you tell me pls,what is the difference between web.close() and web.dispose() methods, and why did you used BOTH of them?
Couldn’t tell you, I don’t know why I just didn’t put it in a using statement. I musta been drunk or something.