Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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>
share|improve this question

2 Answers 2

You can use the SPBuiltInFieldId.Author:

listItem[SPBuiltInFieldId.Author] = new SPFieldUserValue(web, userId, userName);
listItem.SystemUpdate(false);
share|improve this answer
    
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. –  Dushi Sharma Dec 4 '13 at 12:21
    
Then you are in trouble, how the heck would you make sure a display name is unique? ;) –  Robert Lindgren Dec 4 '13 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". –  Dushi Sharma Dec 4 '13 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. –  Robert Lindgren Dec 4 '13 at 12:28
    
Okay. I got your point Robert. But there must be some way by which I can do this. –  Dushi Sharma Dec 4 '13 at 12:30

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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