【问题标题】:Pass by value - List name按值传递 - 列表名称
【发布时间】:2016-01-20 15:21:02
【问题描述】:

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

    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);


        }

    }

我得到的错误是在 context.Load(collListItem);

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

【问题讨论】:

    标签: c# asp.net sharepoint call-by-value


    【解决方案1】:

    异常所说的正是正在发生的事情。您在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
    

    【讨论】:

    • 非常感谢您的回复。你能告诉我怎么做吗?
    【解决方案2】:

    我猜你可能会尝试在 DeleteLastUpdate 方法中重用上下文,该方法是在 GetCountry 方法中获取的。顺便说一句,DeleteLastUpdate 在执行大量查询时似乎无效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 2011-01-20
      • 1970-01-01
      相关资源
      最近更新 更多