代码之家  ›  专栏  ›  技术社区  ›  AndreaNobili

为什么不能通过代码将文件插入SharePoint列表?它变成了例外,似乎访问被拒绝了

  •  0
  • AndreaNobili  · 技术社区  · 7 年前

    SharePoint (我正在使用SharePoint 2013)遇到一个奇怪的问题。这很奇怪,因为在我的应用程序的另一个部分,它工作得很好(在另一个子网站)。

    所以基本上进入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;
    

    然后在这个列表中创建(它不存在)一个geraric文件夹结构,用于年(annoFolder)、月(meseFolder)和日( 日文件夹 ).

    它工作得很好,我试图从我的SharePoint站点删除这些文件夹结构,并执行此方法,它被再次创建,事实上这是我得到的:

    enter image description here

    如您所见,它正确地将此文件夹结构创建到我的SharePoint列表(名为 原生动物 )进入当前网站。

    日文件夹 )签署人:

    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中使用相同代码的另一个站点没有问题。另一件奇怪的事情是,手动将文件上传到列表的这个位置,工作正常,所以我认为这不应该是用户权限问题。

    有什么想法吗?我能做些什么来解决这个奇怪的问题?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Jay    7 年前

    SharePoint代码使用IIS中的应用程序池用户而不是您登录到SharePoint的用户运行,因此即使在 有机会。所以我建议您检查Protocollo库中AppPool帐户的权限。或者你可以用 SPSecurity.RunWithElevatedPrivileges

    当心危险 pitfalls 不过。

    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
            }
        }
    });
    
    推荐文章