c# - 为什么我不能通过代码将文件插入 SharePoint 列表?它进入异常,似乎访问被拒绝
问题描述
我是SharePoint的新手(我使用的是 SharePoint 2013),我遇到了一个奇怪的问题。这很奇怪,因为在我的应用程序的另一部分它工作正常(在另一个子站点中)。
所以基本上进入 SharePoint 我有一个名为Protocollo的SharePoint 列表。
我的代码包含以下几行,将文档(文件)添加到上一个 SharePoint 列表的子文件夹中:
internal static int InsertItem(Dictionary<string, object> prot, Documento doc, bool assignVisibility, bool newItem)
{
int state = 0;
SPListItem item = null;
UOR currUOR = null;
List<UOR> path = new List<UOR>();
SPWeb web = SPContext.Current.Web;
string siglaAOO = web.Properties["sigla_aoo"];
DBConnection dbConfig = ArxeiaProtocollo.Util.ProtUtils.InitializeDBConnection();
dbConfig.Database = siglaAOO;
string username = web.CurrentUser.LoginName;
try
{
SPList list = web.Lists["Protocollo"];
web.AllowUnsafeUpdates = true;
SPFolderCollection folders = list.RootFolder.SubFolders;
SPFolder annoFolder;
DateTime dateProt = Convert.ToDateTime(prot["Data protocollo"]);
try
{
annoFolder = folders[dateProt.Year.ToString()];
}
catch
{
annoFolder = folders.Add(dateProt.Year.ToString());
}
SPFolder meseFolder;
try
{
meseFolder = annoFolder.SubFolders[dateProt.Month.ToString("D2")];
}
catch
{
meseFolder = annoFolder.SubFolders.Add(dateProt.Month.ToString("D2"));
}
SPFolder dayFolder;
try
{
dayFolder = meseFolder.SubFolders[dateProt.Day.ToString("D2")];
}
catch
{
dayFolder = meseFolder.SubFolders.Add(dateProt.Day.ToString("D2"));
}
SPFile spFile = dayFolder.Files.Add(doc.Nome, doc.File, true);
............................................................
............................................................
............................................................
}
如您所见,前面的代码从当前网站检索协议列表,允许通过以下方式对其进行更新:
SPList list = web.Lists["Protocollo"];
web.AllowUnsafeUpdates = true;
然后在这个列表中创建(它不存在)年 (annoFolder)、月 (meseFolder) 和日 ( dayFolder ) 的 gerarcic 文件夹结构。
它工作正常,我尝试从我的 SharePoint 网站中删除这些文件夹结构并执行此方法,它再次创建,事实上这是我得到的:
正如您所看到的,它正确地将这个文件夹结构创建到我的 SharePoint 列表(名为Protocollo)中到当前网站。
好的,最后我的代码尝试通过以下方式将文档插入最后一个子文件夹(dayfolder):
SPFile spFile = dayFolder.Files.Add(doc.Nome, doc.File, true);
我将传递给 Add() 方法:文件名、表示文件的字节数组和真正的布尔值。
The problem is that performing this line I obtain the following exception that is not providing information:
{Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}
然后在我的前端出现一个“拒绝访问”弹出窗口。
奇怪的是,我的 SharePoint 中使用相同代码的其他网站没有问题。另一个奇怪的事情是,手动将文件上传到列表的这个位置它工作正常,所以我认为它不应该是用户权限问题。
有什么想法?我可以尝试做些什么来解决这个奇怪的问题?
解决方案
SharePoint 代码使用 IIS 中的应用程序池用户而不是您已登录到 SharePoint 的用户运行,因此即使您具有访问权限,也经常会收到拒绝访问错误。所以我建议你在 Protocollo 库上检查 AppPool 帐户的权限。或者,SPSecurity.RunWithElevatedPrivileges
如果您信任将运行代码的用户,则可以使用。
不过要小心陷阱。
这是一个示例用法:
Guid siteId = SPContext.Current.Site.ID;
Guid webId = SPContext.Current.Web.ID;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteId))
{
using (SPWeb web = site.OpenWeb(webId))
{
// Your code here
}
}
});
推荐阅读
- azure - 如何在if语句中使用引用资源函数
- android - View.GONE vs View.INVISIBLE 从另一个线程更新 UI
- telegraf - Telegraf:指标中的字段和标签有什么区别?
- postgresql - 在 Postgres 中创建 JSONB 字段存在的部分索引会给我 PG::ProgramLimitExceeded。但为什么?
- uml - 在序列图中转到?
- shopware - 如何访问 Shopware 的 REST api
- c# - 在 C# 中从 UTF-8 转换
- python - 排序方法在 3.x python 中的工作方式与 2.x 不同
- excel - 如何计算两列中满足条件的行数?
- angular - 如何使用命令更改 CLI 或角度版本