[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]根据用户名获取该用户的权限

代码

[sharepoint]修改Item或者File的Author和Editor
                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();
[sharepoint]修改Item或者File的Author和Editor

验证

[sharepoint]修改Item或者File的Author和Editor
        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);
        }
[sharepoint]修改Item或者File的Author和Editor

修改Item类似

[sharepoint]修改Item或者File的Author和Editor
       /// <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();
        }
[sharepoint]修改Item或者File的Author和Editor

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

[sharepoint]修改Item或者File的Author和Editor
       /// <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();
                }
            }
        }
[sharepoint]修改Item或者File的Author和Editor

总结

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

博客地址: http://www.cnblogs.com/wolf-sun/
博客版权: 本文以学习、研究和分享为主,欢迎转载,但必须在文章页面明显位置给出原文连接。
如果文中有不妥或者错误的地方还望高手的你指出,以免误人子弟。如果觉得本文对你有所帮助不如【推荐】一下!如果你有更好的建议,不如留言一起讨论,共同进步!
再次感谢您耐心的读完本篇文章。http://www.cnblogs.com/wolf-sun/p/4756088.html

上一篇:windows中修改catalina.sh上传到linux执行报错This file is needed to run this program


下一篇:ResGen.exe”已退出,代码为2 问题处理