1

Problem: Creating a site column using the SharePoint API (Object Model) with incorrect case in the URL for the SPSite or SPWeb object will cause the new column to throw an exception if an attempt to edit it is made through the Site Columns Gallery.

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

Then, 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
  }
}

Disclaimer: Shameless re-ask from StackOverflow

2 Answers 2

1

I haven't tested these but you could try getting the properly cased url from the various SharePoint web services such as http://www.wssdemo.com/_vti_bin/webs.asmx?op=WebUrlFromPageUrl although if the case isn't correct in the SPSite and SPWeb objects then they may not be correct there.

Alternatively you could create your own web service that takes the url and returns the proper cased url by accessing the current context in the web service. A lot of hoops to go through for something that seems like it should be able to done easier though.

0

I tend to use the SPContext to retrieve the Web and Site objects where applicable.

However, you can only use this where your code has a context, so within Web Parts and application pages, and this rules out command line and timer job stuff.

Basically, I try to avoid hard-coding a URL in code whenever I can avoid it, as it makes the code less scalable and adaptable. What context does your code run in? Event/Feature Receiver, Web Part, etc?

1
  • @James - unfortunately it is in a script/compiled. I had an idea of maybe using the MasterPageUrl field as that seems to contain the correct case sensitive path - but that seems like a super hack.
    – Goyuix
    Jun 24, 2010 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.