SharePoinrt 授權變更 連接 [ClientContext]

SharePoinrt 授權變更 連接 [ClientContext]


    public string TestSharePoint(String paramUrl, String paramGuid, String paramCondition) {
        String _result = "";
        Uri _url = new System.Uri(paramUrl);

        try {  
            using (ClientContext context = new ClientContext(paramUrl)) {

                context.Credentials = new NetworkCredential("帳號", "密碼", "網域");  //授權

                List list = context.Web.Lists.GetById(new Guid(paramGuid));
                ListItemCollection collListItem = list.GetItems(CamlQuery.CreateAllItemsQuery());
                context.Load(collListItem);
                context.ExecuteQuery();

                DataTable dt = new DataTable();
                dt.Columns.Add(new DataColumn("Title"));
                dt.Columns.Add(new DataColumn("Path"));
                dt.Columns.Add(new DataColumn("Guid"));

                foreach (ListItem s in collListItem) {
                    DataRow dr = dt.NewRow();
                    dr[0] = s["Title"] == null ? null : s["Title"];
                    dr[1] = s["FileRef"] == null ? null : String.Format("http://{0}", _url.Authority) + s["FileRef"];
                    dr[2] = s["UniqueId"] == null ? null : s["UniqueId"];
                    dt.Rows.Add(dr);

                }

                //條件查詢 
                DataTable dt1 = null;

                if (paramCondition != "") {
                    var query = from s in dt.AsEnumerable()
                                where s.Field<String>("Path").Contains(paramCondition)
                                select s;
                    dt1 = query.CopyToDataTable();
                } else
                    dt1 = dt;  //當沒下條件時全撈

                foreach (DataRow s in dt1.Rows) {
                    _result = _result + s["Path"].ToString() + ",";  //每段網址使用 , 切割
                }
                _result = _result.Substring(0, _result.Length - 1);
            }
        } catch (Exception ex) {
            _result = ex.ToString();
        }

        return _result;
    }