以编程方式将Sharepoint列表项从一个站点删除到另一个站点

本文关键字:站点 一个 删除 另一个 方式 编程 Sharepoint 列表 | 更新日期: 2023-09-27 18:28:29

如果mySourceList中不存在项目,我将尝试从myDestinationList中删除这些项目。我正试图在C#中完成这个方法。我这样设置是因为这两个列表位于不同的服务器上。有什么建议吗?

我还可以将源列表项中的"作者/编辑器"分配给目标列表中的"作家/编辑器"字段吗?

更新:列表有超过2k个项目,客户端可能每天或每周更新一次列表,这可能会有所不同。

    static void Main(string[] args) {
      ClientContext context = new ClientContext(site2);
      List mySourceList = context.Web.Lists.GetByTitle(ListName);
      CamlQuery camlQuery = new CamlQuery();
      camlQuery.ViewXml = "<View></View>";
      ListItemCollection mySourceItemColl = mySourceList.GetItems(camlQuery);
      context.Load(mySourceItemColl);
      context.ExecuteQuery();
      SPSite myDestinationSite = new SPSite(site2);
      SPWeb myDestinationWeb = myDestinationSite.OpenWeb();
      SPList myDestinationList = myDestinationWeb.Lists[ListName2];
      SPQuery myDestinationListQuery = new SPQuery();
      myDestinationListQuery.Query = "" +
        "" +
        "" +
        "" +
        "";
      SPListItemCollection myDestinationItemColl = myDestinationList.GetItems(myDestinationListQuery);
      foreach(ListItem mySourceListItem in mySourceItemColl) {
        foreach() {
//delete items
          item.Delete();
        }
        foreach(SPListItem myDestinationListItem in myDestinationItemColl) {
//Update items here
}
      }
    }

事件接收器

        public override void ItemAdded(SPItemEventProperties properties) {
            using(ClientContext context = new ClientContext(site1)) {
              List list = context.Web.Lists.GetByTitle(sourceList);
              CamlQuery camlQuery = new CamlQuery();
              camlQuery.ViewXml = "<View></View>";
              ListItemCollection collListItem = list.GetItems(camlQuery);
              context.Load(collListItem);
              context.ExecuteQuery();
              using(ClientContext target = new ClientContext(site1)) {
                List list2 = target.Web.Lists.GetByTitle(destinationList);
                foreach(ListItem oListItem in collListItem) {
                  ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                  ListItem AddListItem = list2.AddItem(itemCreateInfo);
                  AddListItem["Title"] = oListItem["Title"];
                  AddListItem.Update();
                  target.ExecuteQuery();
                }
              }
            }

以编程方式将Sharepoint列表项从一个站点删除到另一个站点

您将如何识别列表项?按标题?这是独一无二的吗?你可以做两件事:

  1. 从源列表和目标中获取所有项目。执行Linq查询并删除差异

当你有很多物品时(尤其是当你有2000多件物品时),这可能会很贵。

  1. 循环浏览Destiantion列表并检查Source列表中是否存在项。如果没有,则删除

这也是一项昂贵的任务,因为Sharepoint不像关系数据库,您可以在几秒钟内循环浏览列表。在Sharepoint列表中循环是很耗时的。

我会选择第一种方法。但我不知道我们目前在谈多少项目。你能更新你的帖子并给我们更多关于列表大小的信息吗?你需要多久做一次?

当你更新一个项目时,你也可以为该项目指定一个作者:

        ClientContext ctx = new ClientContext("https://...");
        List list = ctx.Web.Lists.GetByTitle("Idea");
        ListItemCollection items = list.GetItems(CamlQuery.CreateAllItemsQuery());
        ctx.Load(items); // loading all the fields
        ctx.ExecuteQuery();
        foreach (var item in items)
        {
            ctx.Load(item);
            ctx.ExecuteQuery();
            // important thing is, that here you must have the right type
            // i.e. item["Modified"] is DateTime
            FieldUserValue val = item["Old_Created_By_Person"] as FieldUserValue;
            if (val != null)
            {
                item["Author"] = val;
                // do whatever changes you want
                item.Update(); // important, rembeber changes
                ctx.ExecuteQuery();
                Console.WriteLine("Updating " + item.Id);
            }
        }