按值传递 - 列表名称



有人能告诉我我在这里做错了什么吗?我尝试将列表名称传递给一个将删除列表中所有行的方法:

    public static void DeleteLastUpdate(Microsoft.SharePoint.Client.List oList)
    {
        using (var context = new ClientContext(FrontEndAppUrl))
        {
            var ss = new System.Security.SecureString();
            Array.ForEach("hhh".ToCharArray(), (c) => { ss.AppendChar(c); });
            context.Credentials = new SharePointOnlineCredentials("yyy", ss);
            var web = context.Web;
            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = "<View><Query><OrderBy><FieldRef Name='ID' Ascending='TRUE' /></OrderBy></Query></View>";
            ListItemCollection collListItem = oList.GetItems(camlQuery);
            context.Load(collListItem);
            context.ExecuteQuery();

            foreach (ListItem oListItem in collListItem)
            {
                string i = oListItem["ID"].ToString(); ;
                ListItem ListItemToDelete = oList.GetItemById(i);
                ListItemToDelete.DeleteObject();
                context.ExecuteQuery();
            }
            oList.Update();
        }
    }
    public static void GetCountry()
    {
        using (var context = new ClientContext(FrontEndAppUrl))
        {
            Microsoft.SharePoint.Client.List oList_Country = context.Web.Lists.GetByTitle("LISTNAME");
            DeleteLastUpdate(oList_Country);

        }
    }

我得到的错误是在上下文中。Load(collListItem);

它说 该对象用于与与对象关联的上下文不同的上下文中。我还能如何将列表的值传递给 Delete() 方法?

异常所说的就是正在发生的事情。在GetCountry()上下文中创建oList_Country,然后将其传递给在其他上下文中工作DeleteLastUpdate()

也许您应该考虑通过参数将上下文传递给DeleteLastUpdate()。然后你的代码将变成这样:

public static void DeleteLastUpdate(ClientContext context, Microsoft.SharePoint.Client.List oList)
{
    // You should not create a context here, but use the supplied context
    // using (var context = new ClientContext(FrontEndAppUrl))
    // {
        var ss = new System.Security.SecureString();
        ...
}
public static void GetCountry()
{
    using (var context = new ClientContext(FrontEndAppUrl))
    {
        Microsoft.SharePoint.Client.List oList_Country = context.Web.Lists.GetByTitle("LISTNAME");
        DeleteLastUpdate(context, oList_Country);  // Pass the context

我想您可能会尝试在GetCountry方法中获得的方法中重用上下文DeleteLastUpdate上下文。顺便说一句,DeleteLastUpdate执行大量查询看起来效率低下。

相关内容

  • 没有找到相关文章

最新更新