[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的属性了。默认的就是当前请求的用户。

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

相关文章
|
10月前
|
机器人
Typecho弹出find Input author error怎么解决
Typecho弹出find Input author error怎么解决
61 0
|
程序员 Android开发 iOS开发
iOS开发:Xcode报错“Could not insert new outlet connection:Could not find any...”问题的解决方法
在iOS开发过程中会遇到各种各样的问题,有些时候会遇到一些莫名其妙的报错问题,而且会觉得很无厘头,尤其是苹果的Xcode会出现各种不可抗拒的情况,网速不好会影响Xcode的正常使用,时间长不打开Xcode也会出现问题,一般这种情况下果断关闭Xcode再重新打就好了,那么本篇博文就来分享一下需要关闭Xcode再重新打开就好的情况,一般情况下在没有错误操作的时候还会出现这种错误就一定是Xcode的问题,那么接下来就来说说这种情况。
150 0
How to render S4 Code List extension field into CRM WebUI product search view
This question is asked by Wade. Suppose I have created one extension field in S4 with type CodeList which contains all possible queue type supported by JDK. This extension field must be exposed to CRM Product Search WebUI. POC is done in X3C/504:
How to render S4 Code List extension field into CRM WebUI product search view
why I cannot set SAP UI5 dropdown list as not selected
Created by Wang, Jerry, last modified on Feb 11, 2015
why I cannot set SAP UI5 dropdown list as not selected
|
JavaScript PHP 前端开发
PHPStorm File and Code Template
有时候我们想新建某类型文件的时候,默认出现一些基础代码,而不是空白的。 比如当新建一个 html 文件 基础代码是这样的: 同理,当新建php文件,我希望是这样:
1367 0
|
XML Java 数据格式
Myeclipse中java web.xml报错cvc-complex-type.2.3: Element 'web-app' cannot have character [children], because the type's content type is element- only.
web.xml文件添加servlet访问限制后出现如下错误:   cvc-complex-type.2.3: Element 'web-app' cannot have character [children], because the type's content type is element- only. 翻译:   cvc-complex-type.2.3:元素'web-app'不能包含character [children],因为该类型的内容类型是仅包含元素的。
3806 0