Reputation: 5986
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:
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;
}
}
}
Upvotes: 1
Views: 2063
Reputation: 2490
You can use 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.:
ListItem item = list.AddItem(itemCreateInfo);
//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();
Upvotes: 4