• [sharepoint]修改Item或者File的Author和Editor


    写在前面

    最近项目中调用sharepoint rest api方式获取文件或者Item列表,而用的方式是通过证书请求,在上传文件,或者新建item的时候,默认的用户是在sharepoint端注册的用户,并不能满足需求,需要对其进行修改。因为注册的app,会将该注册的clientid和issurid对应的应用程序作为一个新的用户。关于注册的方式可以参考之前写的几篇文章。

    系列文章

    sharepoint环境安装过程中几点需要注意的地方

    Rest API的简单应用

    rest api方式实现对文档库的管理

    通过WebClient模拟post上传文件到服务器

    WebHttpRequest在sharepoint文档库中的使用

    [sharepoint]Rest api相关知识(转)

    [sharepoint]根据用户名获取该用户的权限

    [sharepoint]根据用户名获取该用户的权限

    代码

                    ClientContext spContext = new ClientContext("http://xxx//xxxx/xxx");
                    spContext.ExecutingWebRequest += spContext_ExecutingWebRequest;
                    Web website = spContext.Web;
                    spContext.Load(website);
                    Microsoft.SharePoint.Client.File file = website.GetFileByServerRelativeUrl(serverReleativeUrl);
                    ListItem item = file.ListItemAllFields;
                    string loginName = @"i:0#.w|domain" + _siteInfo.UserName;
                    User user = spContext.Web.EnsureUser(loginName);
                    item["Editor"] = user;
                    item["Author"] = user;
                    file.ListItemAllFields.Update();
                    spContext.ExecuteQuery();

    验证

            public void spContext_ExecutingWebRequest(object sender, WebRequestEventArgs e)
            {
                HttpWebRequest webReq = e.WebRequestExecutor.WebRequest;
                TokenHelper.ClientId = _siteInfo.ClientId;
                TokenHelper.IssuerId = _siteInfo.IssureId;
                var accessToken = TokenHelper.GetS2SAccessTokenWithWindowsIdentity(new Uri(_siteInfo.AppUrl), null);
                webReq.Method = "Post";
                webReq.Accept = "application/json;odata=verbose";
                webReq.Headers.Add("Authorization", "Bearer " + accessToken);
            }

    修改Item类似

           /// <summary>
            /// 修改Item的作者和编辑者
            /// </summary>
            /// <param name="siteInfo"></param>
            /// <param name="itcode"></param>
            /// <param name="itemId"></param>
            public static void UpdateItemAuthor(SiteInfo siteInfo, string itcode, int itemId)
            {
                ClientContext spContext = new ClientContext(siteInfo.Url);
                spContext.ExecutingWebRequest += spContext_ExecutingWebRequest;
                Web web = spContext.Web;
                spContext.Load(web);
                spContext.ExecuteQuery();
                List lst = web.Lists.GetById(new Guid(siteInfo.ListId));
                spContext.Load(lst);
                spContext.ExecuteQuery();
                ListItem item = lst.GetItemById(itemId);
                string loginName = @"i:0#.w|lenovo" + itcode;
                User user = web.EnsureUser(loginName);
                item["Author"] = user;
                item["Editor"] = user;
                item.Update();
                spContext.ExecuteQuery();
            }

    在修改或者新建item或者File的时候需要先获取contextinfo

           /// <summary>
            /// 获得contextinfo
            /// </summary>
            /// <param name="siteInfo"></param>
            /// <returns></returns>
            private static string GetContextInfo(SiteInfo siteInfo)
            {
                HttpWebRequest contextInfoRequest = null;
                HttpWebResponse contextInfoEndpointResponse = null;
                StreamReader contextInfoStream = null;
                try
                {
                    Uri hostWebUri = new Uri(siteInfo.Url);
                    TokenHelper.ClientId = siteInfo.ClientId;
                    TokenHelper.IssuerId = siteInfo.IssureId;
                    var accessToken = TokenHelper.GetS2SAccessTokenWithWindowsIdentity(hostWebUri, null);
                    //获取contextinfo
                    contextInfoRequest = (HttpWebRequest)HttpWebRequest.Create(siteInfo.Url + "/_api/contextinfo");
                    contextInfoRequest.Method = "POST";
                    contextInfoRequest.Accept = "application/json;odata=verbose";
                    contextInfoRequest.ContentLength = 0;
                    contextInfoRequest.Headers.Add("Authorization", "Bearer " + accessToken);
                    contextInfoEndpointResponse = (HttpWebResponse)contextInfoRequest.GetResponse();
                    contextInfoStream = new StreamReader(contextInfoEndpointResponse.GetResponseStream(), Encoding.UTF8);
                    string strContextInfoJson = contextInfoStream.ReadToEnd();
                    return strContextInfoJson;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    if (contextInfoEndpointResponse != null)
                    {
                        contextInfoEndpointResponse.Dispose();
                    }
                    if (contextInfoStream != null)
                    {
                        contextInfoStream.Dispose();
                    }
                }
            }

    总结

    如果在请求的时候能带上账户信息,是最简单的一种方式,默认的就是当前的用户信息。比如spContext.Credentials =new NetWorkCredentials (uid,pwd,domain);或者在模拟请求的时候request.Credentials =new NetWorkCredentials (uid,pwd,domain);这种方式就不用再次请求进行修改文件或者item的属性了。默认的就是当前请求的用户。

  • 相关阅读:
    02-17 位图验证码(一般处理程序)+AJAX
    02-18 报表
    SQLite 函数大全
    SQLite中的时间日期函数(转)
    DES,AeS加解密,MD5,SHA加密
    suspendlayout
    AES--高级数据加密标准
    C#中Validating和Validated事件
    Net操作Excel(终极方法NPOI)
    decimal,float和double的区别
  • 原文地址:https://www.cnblogs.com/wolf-sun/p/4756088.html
Copyright © 2020-2023  润新知