有没有办法使用Microsoft.SharePoint.Client命名空间

时间:2017-12-20 12:41:43

标签: c# .net sharepoint sharepoint-clientobject

我们有一个SharePoint列表,其中每个用户都有权查看他创建的记录。

我有一项任务是编写一个生成列表项的服务, 列表项必须由另一个用户创建,而不是由服务器的经过身份验证的用户创建(出于上面提到的原因),以便由创建"创建的用户查看项目。这个项目。

问题:我们尝试了很多,我们无法将列表项的创建者更改为另一个用户,而不是经过身份验证的服务器用户(不会抛出异常并且项目已成功创建)。我们怎样才能做到这一点?


约束:

  • 该服务将在安装了SharePoint服务器 的服务器上运行。
  • 我们正在使用Microsoft.SharePoint.Client命名空间,但如果需要,我们也可以使用SharePoint SOAP服务。

这是代码的草稿:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using System.Security;
using Microsoft.SharePoint.Client.UserProfiles;
using System.Net;

namespace SharePointClientSaveWithItherUser
{
    class Program
    {
        static void Main(string[] args)
        {
            UpdateListItem();
        }

        public static void UpdateListItem()
        {
            var siteURL = "http://oportalXXXXXXXXXXXXX";
            var listName = "XXXXX";
            var createdBy = "XXXXXX";
            var modifiedBy = "XXXXXX";

            ClientContext context = new ClientContext(siteURL);

            var login = "XXXXXX";
            var password = "XXXXXX!";

            var securePassword = new SecureString();
            foreach (char c in password)
            {
                securePassword.AppendChar(c);
            }


       //     SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(login, securePassword);
            context.Credentials = new NetworkCredential(login, securePassword, "XXXX"); ;

            List list = context.Web.Lists.GetByTitle(listName);
            FieldUserValue author = GetUsers(context, createdBy);
            FieldUserValue editor = GetUsers(context, modifiedBy);
            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
            ListItem oListItem = list.AddItem(itemCreateInfo);
            oListItem["Title"] = "TEST";
            oListItem["Author"] = author; // its not working the creator of the record is the authanticated user and not the author 
           // oListItem["Editor"] = editor;

            oListItem.Update();
            context.ExecuteQuery();
        }

        //get user by username
        public static FieldUserValue GetUsers(ClientContext clientContext, string UserName)
        {

            FieldUserValue _userValue = new FieldUserValue();

            User _newUser = clientContext.Web.EnsureUser(UserName);

            clientContext.Load(_newUser);

            clientContext.ExecuteQuery();

            _userValue.LookupId = _newUser.Id;

            return _userValue;

        }
    }
}

1 个答案:

答案 0 :(得分:2)

您可以使用EnsureUser方法并将用户的电子邮件地址(user.name@domain.com)或域名(i:0#.w|domain\username)传递给它以解析用户,然后将其分配给作者字段。:

ListItem item = list.AddItem(itemCreateInfo);
//use email address of user or login name
var emailAddress = "user.name@tenantname.com"  //or  i:0#.w|domain\username;

var user = context.Web.EnsureUser(emailAddress);
context.Load(user);
context.ExecuteQuery();

ListItem oListItem = list.AddItem(itemCreateInfo);
oListItem["Title"] = "TEST";
oListItem["Author"] = user;                    

oListItem.Update();
context.ExecuteQuery();