C# 连接 SharepointOnline WebService

使用Sharepoint Client对象模型进行数据读取:

  ClientContext clientContext = new ClientContext(ConfigHelper.GetConfigStr("SharepointOnlineURL"));
                string strPassWord = ConfigHelper.GetConfigStr("SharepointOnlinePW");
                char[] pChar = strPassWord.ToCharArray();
                SecureString password = new SecureString();
                foreach (char c in pChar)
                {
                    password.AppendChar(c);
                }
               clientContext.Credentials = new SharePointOnlineCredentials(ConfigHelper.GetConfigStr("SharepointOnlineUser"), password);
                List a = clientContext.Web.Lists.GetByTitle("邮件分发定义");
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name=\"UseJob\"></FieldRef><Value Type=\"Text\">" + JobName + "</Value></Eq></Where></Query></View>";
                Microsoft.SharePoint.Client.ListItemCollection Listitems = a.GetItems(camlQuery);
                clientContext.Load(
                 Listitems,
                 items => items
                     .Include(
                         item => item["ReportName"],
                         item => item["Addressee"],
                         item => item["UseJob"],
                         item => item["ExcelName"],
                         item => item["AccountNumber"]
                         ));
                clientContext.Load(Listitems);
                clientContext.ExecuteQuery();
                DataTable dt = new DataTable();
                dt.Columns.Add("ReportName", Type.GetType("System.String"));
                dt.Columns.Add("Addressee", Type.GetType("System.String"));
                dt.Columns.Add("UseJob", Type.GetType("System.String"));
                dt.Columns.Add("ExcelName", Type.GetType("System.String"));
                dt.Columns.Add("AccountNumber", Type.GetType("System.String"));
                foreach (Microsoft.SharePoint.Client.ListItem listItem in Listitems)
                {
                    DataRow newRow;
                    newRow = dt.NewRow();
                    newRow["ReportName"] = listItem["ReportName"].ToString(); 
                    newRow["Addressee"] = listItem["Addressee"].ToString();
                    newRow["UseJob"] = listItem["UseJob"].ToString();
                    newRow["ExcelName"] = listItem["ExcelName"].ToString();
                    newRow["AccountNumber"] = listItem["ExcelName"].ToString();
                    dt.Rows.Add(newRow);
                }

                string result = JsonConvert.SerializeObject(dt);
                return result;

其实最重要的验证身份凭据,验证方式和WebService的有点不同:

 clientContext.Credentials = new SharePointOnlineCredentials(ConfigHelper.GetConfigStr("SharepointOnlineUser"), password);

用的是:SharePointOnlineCredentials

猜你喜欢

转载自blog.csdn.net/qq_23502409/article/details/76152675