1

I'm trying to import data from an .xml file to a custom list in SP2010(created programmatically).

I'm able to create and update all fields except 'Created By' as it says that "Cannot update read only field".

Is there any way that I can update the "Created By" field? Only Programmatically

This is my XML code.

Note : I don't have user Id. I only have Display Name. I need field from xml code to be updated in SP.

<?xml version="1.0" encoding="iso-8859-1" ?>

<XMLExport DBName="local_Report DB">

<documentexport DOCID="DJHD7WJWA9APWPWO457C20004FE01A">

<Business>

<![CDATA[ Bank Subsidiary]]>

</Business>

<Company>

<![CDATA[ DMS Softies, India ]]>

</Company>

<Region />

<Author>

<![CDATA[ Dushyant M Sharma]]>

</Author>

</documentexport>

</XMLExport>

2 Answers 2

4

You can use the SPBuiltInFieldId.Author:

listItem[SPBuiltInFieldId.Author] = new SPFieldUserValue(web, userId, userName);
listItem.SystemUpdate(false);
10
  • Hi Robert. Thanks for your suggestion. But The problem is that I don't have any userId or userName. I only have Display Name that I'm exporting from Lotus Notes DB to XML file. Commented Dec 4, 2013 at 12:21
  • Then you are in trouble, how the heck would you make sure a display name is unique? ;) Commented Dec 4, 2013 at 12:23
  • At this moment I'm not considering uniqueness. I just want SP to somehow match that Display Name from Active Directory. If it is not present in Active Directory then it should update 'Created By' with "System Account". Commented Dec 4, 2013 at 12:28
  • Well, display name i AD is in no way unique, so you can not in a secure way identify a specific user from it. Commented Dec 4, 2013 at 12:28
  • Okay. I got your point Robert. But there must be some way by which I can do this. Commented Dec 4, 2013 at 12:30
0

you can also do:

SPFieldUserValue user = new SPFieldUserValue(Web, Web.CurrentUser.ID, Web.CurrentUser.LoginName);   

listItem["Author"] = user;
listItem["Editor"] = user;

listItem.Update(); 

web.Update();

SPFieldUserValue can be hard coded or as iv done getting the current user, web is the SPWeb object and listItem is the SPListItem object

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.