【发布时间】:2011-01-28 09:07:50
【问题描述】:
问题: 使用 SharePoint API(对象模型)在 SPSite 或 SPWeb 对象的 URL 中大小写不正确的情况下创建网站栏将导致新栏在尝试编辑时引发异常它是通过网站专栏库制作的。
SharePoint 通常非常容忍以不区分大小写的方式接受 URL,但是在少数情况下它会完全崩溃。例如,在创建站点栏时,它以某种方式存储和使用创建时的 URL,当尝试通过站点栏库(布局中的 fldedit.aspx 页面)编辑字段定义时,您最终会抛出以下错误。
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)
如何可靠地获取站点/网站的正确 URL? SPSite.Url 和 SPWeb.Url 属性似乎会返回它们被实例化的任何情况。
换句话说,网站集是使用以下 URL 配置的:http://server/Path/Site
然后,如果我使用 SharePoint 对象模型创建一个新的网站栏,并且在实例化 SPSite 和 SPWeb 对象时碰巧使用了http://server/path/site,则该网站栏将可用,但在尝试通过库访问它时出现错误上面是生成的。如果我更正地址栏中的 URL,我仍然可以查看/修改相关 SPField 的定义,但生成的默认 URL 是伪造的。
清如泥?
示例代码:(由于区分大小写问题,这是一个不好的示例)
// 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
}
}
【问题讨论】:
-
您在哪里运行创建网站栏的代码?通过特征接收器?
-
@UnhipGlint 我目前只是在服务器上的控制台应用程序中运行它。最终它可能会在功能接收器或服务器控件/.ashx 中被按需调用。
标签: sharepoint moss wss