CSOM/Console application /powershell script to insert data in a sharepoint list by reading email IDs from an excel but created by and  modified should be email ID user

Hi All,

Please let me know I want to insert data in a list by reading this excel which have email IDS like below-

amir.khan@org.com

Sushmita.rao@org.com...etc

my code/script will pick the email IDs , on basis of email id i can get firstname,middle name, last name .

While inserting/creating entries in list I need to set below list column values-

Title(col1) | Email(Yes/No type col)| AffectedLine (Choice col) |Created By (Default col) | Modified By(def col)

Subscription | Yes | All | Amir,Khan | Amir, Khan

Subscription | Yes | All | Sushmita,Rao | Sushmita,Rao

CSOM/Console application /powershell script to insert data in a sharepoint list by reading email IDs from an excel but created by and  modified should be email ID

September 11th, 2015 1:29am

Hello,

You can't set email address to those default column because system will not accept email. However you can change the display type of those column from list settings. go to list settings-->select column-->change type from show field dropdown.

Now to add/update user into those columns from excel you have to use below code.

SPUser user = web.EnsureUser("userID");

Here is the link to add user by CSOM:

http://blogs.msdn.com/b/kaevans/archive/2013/11/30/setting-a-sharepoint-person-or-group-field-value-with-csom.aspx

Hope it could help

Free Windows Admin Tool Kit Click here and download it now
September 11th, 2015 3:14am

Hi Hemendra,

Through email i will get User id ,names then can I set those names or user id's in created by and Modified columns by csom only? ?

using their internal names Created by-Author, Modified by-..........

September 11th, 2015 11:16am

Hi,

The following code snippet for your reference:

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

	public static void UpdateListItem()
	{
		var siteURL = "http://sharepointurl";
		var listName = "CustomList";
		var listId = 1;
		var createdBy="Dennis Guo";
		var modifiedBy = "Dennis Guo"; 

		ClientContext context = new ClientContext(siteURL);

		//Create SharePoint Online credentials
		//var login = "[username]@[tenant].onmicrosoft.com";
		//var password = "[password]";
		
		//var securePassword = new SecureString();
		//foreach (char c in password)
		//{
		//	securePassword.AppendChar(c);
		//}
		//SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(login, securePassword);

		//Create SharePoint On-premise credentials
		NetworkCredential credentials1 = new NetworkCredential("[Username]", "[Password]", "[Domain]");

		context.Credentials = credentials;
		List list = context.Web.Lists.GetByTitle(listName);
		ListItem item = list.GetItemById(listId);
		FieldUserValue author = GetUsers(context, createdBy);
		FieldUserValue editor = GetUsers(context, modifiedBy);
		item["Author"] = author;
		item["Editor"] = editor;

		item.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;
	}
}

Best Regards,

Dennis

Free Windows Admin Tool Kit Click here and download it now
September 13th, 2015 10:25pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics