欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# 连接 SharepointOnline WebService

程序员文章站 2024-03-15 14:14:17
...

使用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

更多文章请扫码关注公众号,有问题的小伙伴也可以在公众号上提出哦。

C# 连接 SharepointOnline WebService

相关标签: sharepoint

上一篇: ros Catkin工作空间

下一篇: