Problem: Creating a site column using the SharePoint API (object model) with an incorrect case in the URL for the SPSite or SPWeb object will cause the new column to throw an exception if an attempt to change it is created through the site column gallery.
SharePoint is generally very tolerant of accepting a case-insensitive URL, but there are a few cases where it breaks completely. For example, when creating a site column, it somehow saves and uses the URL when it was created, and when you try to change the field definition through the site column gallery (fldedit.aspx page in LAYOUTS), you end up throwing an 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 / network? The SPSite.Url and SPWeb.Url properties seem to return all the cases in which they were created.
In other words, the site collection is provided using the following URL: http: // server / path / site
Then, if I create a new site column using the SharePoint object model and use http: // server / path / site when creating SPSite and SPWeb objects, the site column will be available, but when I try to access it through the gallery, the error above is generated. If I correct the URL in the address bar, I can still view / change the definition for the SPField in question, but the default URL created is fictitious.
Remove like dirt?
Code example: (this is a bad example due to case sensitivity problem)
// 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 } }
source share