Company      |       Articles & Research      |      Services      |      Software      |      Contact

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:

C#:
  1. public static CultureInfo GetLocale(string url)
  2. {
  3. SPWeb web = GetWeb(url);
  4. CultureInfo locale = web.Locale;
  5. web.Close();
  6. web.Dispose();
  7. return locale;
  8. }

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.

C#:
  1. public static SPWeb GetWeb(string url)
  2. {
  3. SPSite site = new SPSite(url);
  4. return site.OpenWeb();
  5. }

Happy geo-developing (that should be a word).

share save 171 16 Get Locale From SPWeb URL

Related posts:

  1. Geo-Compliant Layouts Coding
  2. Getting a SharePoint Field Value In C#
  3. Always Test Whether Your GUID’s Are Valid!
  4. Existence of Folder and Files in SharePoint
  5. Getting GAC Path Using Reflection

2 Comments »

  1. 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?

    Comment by Dmitry — March 4, 2009 @ 7:40 am

  2. 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.

    Comment by Adam Buenz — March 4, 2009 @ 8:37 am

RSS feed for comments on this post. TrackBack URL

Leave a comment