C# Sharepoint 读取和更新

安装Microsoft.SharePointOnline.CSOM

1.根据sapno,更新状态

            List<ListItem> listAll = new List<ListItem>();
            using (ClientContext clientContext = new ClientContext(siteURL))
            {
                SecureString secureString = new SecureString();
                foreach (char c in DecryptAES.Decrypt(password).ToCharArray()) secureString.AppendChar(c);
                clientContext.Credentials = new SharePointOnlineCredentials(DecryptAES.Decrypt(username), secureString);
                List vendorList = clientContext.Web.Lists.GetByTitle("Information");
                CamlQuery camlQuery = new CamlQuery();

                foreach (DataRow dr in dt.Rows)
                {
                    string sapno = dr[0] == null ? "" : dr[0].ToString().Trim().Replace("\r\n", "");
                    if (!string.IsNullOrEmpty(sapno))
                    {
                        camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='SAP_x002d_Code'/><Value Type='Text'>" + sapno + "</Value></Eq></Where></Query></View>";
                        ListItemCollection listItems = vendorList.GetItems(camlQuery);
                        clientContext.Load(listItems);
                        clientContext.ExecuteQuery();
                        if (listItems.Count > 0)
                        {
                            foreach (ListItem item in listItems)
                            {
                                if (item["_x72b6__x6001_"].ToString() == "禁用")
                                {
                                    LoggerHelper.Monitor("已被禁用: ID=" + item["ID"] + ",VendorName=" + item["_x4f9b__x5e94__x5546__x4e2d__x65"] + ",SAPCode=" + item["SAP_x002d_Code"]);
                                }
                                else
                                {
                                    item["_x72b6__x6001_"] = "禁用";
                                    item.Update();
                                    clientContext.ExecuteQuery();
                                }
                            }
                        }
                        else
                        {
                            LoggerHelper.Monitor("未查询到:" + sapno);
                        }
                    }
                }
            }

2.读取sharepoint list数据(循环,每次读取500行)

        public static List<ListItem> GetData(string siteUrl, string viewTitle)
        {
            try
            {
                List<ListItem> listAll = new List<ListItem>();
                using (ClientContext clientContext = new ClientContext(siteUrl))
                {
                    SecureString secureString = new SecureString();
                    foreach (char c in DecryptAES.Decrypt(password).ToCharArray()) secureString.AppendChar(c);
                    clientContext.Credentials = new SharePointOnlineCredentials(DecryptAES.Decrypt(username), secureString);

                    List vendorList = clientContext.Web.Lists.GetByTitle(viewTitle);
                    ListItemCollectionPosition itemPosition = null;

                    while (true)
                    {
                        CamlQuery camlQuery = new CamlQuery();
                        camlQuery.ListItemCollectionPosition = itemPosition;
                        camlQuery.ViewXml = @"<View><RowLimit>500</RowLimit></View>";
                        //camlQuery.ViewXml = "<View Scope=\"RecursiveAll\"></View>";
                        ListItemCollection listItems = vendorList.GetItems(camlQuery);

                        clientContext.Load(listItems);
                        clientContext.ExecuteQuery();

                        itemPosition = listItems.ListItemCollectionPosition;
                        Console.WriteLine(itemPosition);
                        var list = listItems.ToList();
                        listAll.AddRange(list);

                        if (itemPosition == null)
                            break;

                        Console.WriteLine(itemPosition.PagingInfo);
                    }
                }
                return listAll;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

3.获取最近更新过的数据

        public static List<ListItem> GetIncData(string siteUrl, string viewTitle)
        {
            try
            {
                string offset = ConfigurationManager.AppSettings["offset"];
                List<ListItem> listAll = new List<ListItem>();
                using (ClientContext clientContext = new ClientContext(siteUrl))
                {
                    SecureString secureString = new SecureString();
                    foreach (char c in DecryptAES.Decrypt(password).ToCharArray()) secureString.AppendChar(c);
                    clientContext.Credentials = new SharePointOnlineCredentials(DecryptAES.Decrypt(username), secureString);

                    List vendorList = clientContext.Web.Lists.GetByTitle(viewTitle);
                    ListItemCollectionPosition itemPosition = null;

                    CamlQuery camlQuery = new CamlQuery();
                    camlQuery.ListItemCollectionPosition = itemPosition;
                    //camlQuery.ViewXml = "<View><Query><Where>< Geq >< FieldRef Name =\"Modified\" />< Value Type = \"DateTime\" ><Today  OffsetDays = \"-1 \"></ Today ></ Value ></ Geq ></ Where ></ Query >< RowLimit>500</RowLimit></View>";

                    camlQuery.ViewXml = "<View><Query><Where><Geq><FieldRef Name='Modified'/><Value Type='DateTime'><Today OffsetDays='" + offset + "'/></Value></Geq></Where></Query></View>";

                    //camlQuery.ViewXml = "<View Scope=\"RecursiveAll\"></View>";
                    ListItemCollection listItems = vendorList.GetItems(camlQuery);

                    clientContext.Load(listItems);
                    clientContext.ExecuteQuery();

                    //itemPosition = listItems.ListItemCollectionPosition;
                    //Console.WriteLine(itemPosition);
                    var list = listItems.ToList();
                    listAll.AddRange(list);

                    //if (itemPosition == null)
                    //    break;

                    //Console.WriteLine(itemPosition.PagingInfo);
                }
                return listAll;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

posted @ 2022-10-24 15:56  highlightyys  阅读(168)  评论(0)    收藏  举报