4

We have a SharePoint list in which each user has permission to view only records created by him.

I have a task to write a service that generates list items, list item must be created by another user than the authenticated user of the server (for the reason mentioned in the beginning) in order to view the item by the user who "created" the item.

problem: we tried a lot and we cant change the creator of the list item to another user than the server user that is authenticated (no exception is thrown and the item is created successfully). how can we achieve that?


constrains:

  • the service will run on a server that SharePoint server not installed on.
  • we are using Microsoft.SharePoint.Client namespace but we also can use SharePoint SOAP service if needed.

that is a draft of the code:

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
  • Have you looked into using a service principal? You can create a new one using the appregnew.aspx page. You can then create a ClientContext using the client ID and client secret using the OfficeDevPnP.Core.AuthenticationManager.GetAppOnlyAuthenticatedContext() Commented Dec 20, 2017 at 17:49

1 Answer 1

2

You can simply use the EnsureUser method and pass the user's email address ([email protected]) or domain name (i:0#.w|domain\username) to it to resolve the user and then assign it to the author field as below:

//use email address of user or login name
var emailAddress = "[email protected]"  //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();
4
  • its works great but this method will run as a wcf service from out app server so we need to use context.Credentials = new System.Net.NetworkCredential("xxxx", "xxxx!", "xxx"); in order to authenticate, this line of code is "overriding" the username of EnsureUser() method and the Author is returning to be the server user...is there a way you know from your experience to overcome this problem? ` Commented Dec 21, 2017 at 13:53
  • BTW if i use context.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; on my pc it works and i can create new item behalf of other user...i wonder whats the reason... Commented Dec 21, 2017 at 14:12
  • @jonathana - unfortunately havent come across such as scenario :( Commented Dec 22, 2017 at 6:52
  • now its working perfect!!! when i tested the service on my pc i had this problem but when i uploaded the service to the server the problem was gone and its working as expected (i wonder why..). thank you very much again!!! Commented Dec 22, 2017 at 7:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.