page.CheckOut() 抛出 SPException:即使页面存在,URL 也是无效的
page.CheckOut() throws SPException: URL is invalid even though page exists
我正在为 SharePoint 网站编写事件接收器,我希望此接收器在基本页面创建后编辑其内容。这是给我带来问题的函数:
public void FillPage(SPSite site, SPItemEventProperties properties, string pageName)
{
using (site)
{
// Wait until the page has been generated
while (!PageExists(properties.BeforeUrl))
{
Thread.Sleep(10000);
}
Thread.Sleep(30000); // Added so I can check that the URL exists in my browser
SPWeb web = site.RootWeb;
SPFile page = web.GetFile(properties.BeforeUrl);
page.CheckOut(); // Throws SPException: 'URL is invalid'.
...
}
}
PageExists 函数仅使用指向刚刚生成的页面的 HttpWebRequest:
public bool PageExists(string url_ending)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri((the root site URL) + url_ending));
request.Timeout = 15000;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return true;
}
catch (WebException we)
{
if (we.Message.Contains("Unauthorized"))
{
return true; // If it's an authorization error, the page exists but access was denied
}
return false;
}
}
CheckOut 函数 returns: "SPException: The URL '...' is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web." 此外,我在包含 'page.Checkout()' 的行添加了一个断点并检查了该页面变量,发现它的所有成员都抛出一个'System.IO.FileNotFoundException' 或 'System.IndexOutOfRangeException',即使它指向正确的 URL。我还检查了 HttpWebRequest 是否指向正确的 URL,正如评论中所提到的那样,我检查该页面是否存在于我的浏览器中,然后代码才能尝试检查它。
根据我所做的搜索,我发现这个错误经常在数据库日志填满时抛出。但根据我的发现,在这种情况下,尝试从 SharePoint 站点本身签出文档时也会发生此错误,而我没有遇到过该问题;当我尝试从事件接收器检出页面时,我只会收到此错误。知道发生了什么事吗?
我找到了这篇文章
http://blog.mastykarz.nl/inconvenient-spwebgetfilestring/
这说明 GetFile 可能会产生意想不到的结果。
提供了一个解决方法:
using (SPSite site = new SPSite("http://moss"))
{
using (SPWeb web = site.RootWeb)
{
object o = web.GetFileOrFolderObject("/site/subsite1/Pages/default.aspx");
if (o is SPFile)
{
SPFile f = (SPFile)o;
}
}
}
你应该试一试!
我正在为 SharePoint 网站编写事件接收器,我希望此接收器在基本页面创建后编辑其内容。这是给我带来问题的函数:
public void FillPage(SPSite site, SPItemEventProperties properties, string pageName)
{
using (site)
{
// Wait until the page has been generated
while (!PageExists(properties.BeforeUrl))
{
Thread.Sleep(10000);
}
Thread.Sleep(30000); // Added so I can check that the URL exists in my browser
SPWeb web = site.RootWeb;
SPFile page = web.GetFile(properties.BeforeUrl);
page.CheckOut(); // Throws SPException: 'URL is invalid'.
...
}
}
PageExists 函数仅使用指向刚刚生成的页面的 HttpWebRequest:
public bool PageExists(string url_ending)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri((the root site URL) + url_ending));
request.Timeout = 15000;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return true;
}
catch (WebException we)
{
if (we.Message.Contains("Unauthorized"))
{
return true; // If it's an authorization error, the page exists but access was denied
}
return false;
}
}
CheckOut 函数 returns: "SPException: The URL '...' is invalid. It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web." 此外,我在包含 'page.Checkout()' 的行添加了一个断点并检查了该页面变量,发现它的所有成员都抛出一个'System.IO.FileNotFoundException' 或 'System.IndexOutOfRangeException',即使它指向正确的 URL。我还检查了 HttpWebRequest 是否指向正确的 URL,正如评论中所提到的那样,我检查该页面是否存在于我的浏览器中,然后代码才能尝试检查它。
根据我所做的搜索,我发现这个错误经常在数据库日志填满时抛出。但根据我的发现,在这种情况下,尝试从 SharePoint 站点本身签出文档时也会发生此错误,而我没有遇到过该问题;当我尝试从事件接收器检出页面时,我只会收到此错误。知道发生了什么事吗?
我找到了这篇文章
http://blog.mastykarz.nl/inconvenient-spwebgetfilestring/
这说明 GetFile 可能会产生意想不到的结果。
提供了一个解决方法:
using (SPSite site = new SPSite("http://moss"))
{
using (SPWeb web = site.RootWeb)
{
object o = web.GetFileOrFolderObject("/site/subsite1/Pages/default.aspx");
if (o is SPFile)
{
SPFile f = (SPFile)o;
}
}
}
你应该试一试!