无法访问站点共享点内的文件名称

时间:2016-05-03 21:14:08

标签: c# sharepoint sharepoint-2013

我有方法在哪里向网站添加新文件。我正在尝试实现一种方法,我可以从站点中删除文件。目前该方法是从最旧到最新删除文件。我希望有权访问NameDisplayName以执行if检查。当我尝试访问listitem.DisplayName时,我遇到了错误:

“属性或字段'DisplayName'尚未初始化。尚未请求或请求尚未执行。可能需要明确请求。”

如果有人可以帮助我了解如何允许listitem.DisplayName访问,我将非常感谢!

DeleteAFile()

 public static void DeleteAFile(string sFileName, string sFldrLoc)
    {

        ClientContext clientContext = new ClientContext(siteURL);

        List spList = clientContext.Web.Lists.GetByTitle(sFldrLoc);


        CamlQuery query = new CamlQuery();

        ListItemCollection listItems = spList.GetItems(query);
        clientContext.Load(listItems);
        clientContext.ExecuteQuery();



        foreach (ListItem listitem in listItems)
        {
            Console.WriteLine(listitem.DisplayName); // Line that is throwing error

           // listitem.DeleteObject();
            clientContext.ExecuteQuery();
        }

    }

2 个答案:

答案 0 :(得分:2)

<强>解决

感谢 Thriggle 获取帮助我解决此问题的链接。更新了以下代码:

<强> DeleteAFile()

    /// <summary>
    /// Will list out all the items within a Site, conduct a search and delete the item when found.
    /// </summary>
    /// <param name="sFileName"></param>
    /// <param name="sFldrLoc"></param>
    public static void DeleteAFile(string sFileName, string sFldrLoc)
    {

        ClientContext clientContext = new ClientContext(siteURL);

        Web web = clientContext.Web;
        ListCollection collList = web.Lists;

        List oList = collList.GetByTitle(sFldrLoc);

        CamlQuery query = new CamlQuery();

        query.ViewXml = "<View><Query><Where><Leq>" +
            "<FieldRef Name='ID'/><Value Type='Number'>100</Value>" +
            "</Leq></Where></Query><RowLimit>50</RowLimit></View>";

        ListItemCollection collListItem = oList.GetItems(query);

        clientContext.Load(collListItem,
            items => items.IncludeWithDefaultProperties(
                item => item.DisplayName));
        clientContext.ExecuteQuery();


        foreach (ListItem listitem in collListItem)
        {
            if (listitem.DisplayName.Equals(sFileName))
            {
                listitem.DeleteObject();
                clientContext.ExecuteQuery();

                Console.WriteLine("{0}, has been deleted sucessfully!", listitem.DisplayName);
            }

        }
    }

答案 1 :(得分:1)

您可以更新clientContext.Load的调用,以指明要为每个列表项检索哪些属性。

clientContext.Load(listItems, 
    items => items.IncludeWithDefaultProperties(
    item => item.DisplayName));

供参考,请参阅How to retrieve list items