How to determine the correct (case sensitive) URL for a SharePoint site

Posted by Goyuix on Stack Overflow See other posts from Stack Overflow or by Goyuix
Published on 2010-03-17T00:03:53Z Indexed on 2010/03/17 13:41 UTC
Read the original article Hit count: 510

Filed under:
|
|

SharePoint is generally very tolerant of accepting a URL in a case-insensitive fashion, however there are a few cases where it completely breaks down. For example, when creating a site column it somehow stores and uses the URL when it was created, and when trying to edit the field definition through the Site Column Gallery (fldedit.aspx page in the LAYOUTS) you end up throwing the error below.

Value does not fall within the expected range.
at Microsoft.SharePoint.SPFieldCollection.GetFieldByInternalName(String strName, Boolean bThrowException)
at Microsoft.SharePoint.SPFieldCollection.GetFieldByInternalName(String strName) 
at Microsoft.SharePoint.ApplicationPages.BasicFieldEditPage.OnLoad(EventArgs e) 
at System.Web.UI.Control.LoadRecursive() 
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) 

How can I reliably get the correct URL for a site/web? The SPSite.Url and SPWeb.Url properties seem to return back whatever case they are instantiated with.

In other words, the site collection is provisioned using the following URL: http://server/Path/Site

If I create a new Site Column using the SharePoint object model and happen to use http://server/path/site when instantiating the SPSite and SPWeb objects, the site column will be made available but when trying to access it through the gallery the error above is generated. If I correct the URL in the address bar, I can still view/modify the definition for the SPField in question, but the default URL that is generated is bogus.

Clear as mud?

Example code: (this is a bad example because of the case sensitivity issue)

// note: site should be partially caps: http://server/Path/Site
using (SPSite site = new SPSite("http://server/path/site")) {
  using (SPWeb web = site.OpenWeb()) {
    web.Fields.AddFieldAsXml("..."); // correct XML really here
  }
}

© Stack Overflow or respective owner

Related posts about sharepoint

Related posts about wss