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 am trying to programmatically update some metadata for a sharepoint item using powershell. Here is my code:

$web = getWebByURL("http://test-site/spadmin/")
$itm = $web.lists["Documents"].items[0]
$itm["Description"] = "This is a test edit"
$spfields = "Modified", "Created"
    foreach($fieldname in $spfields){
        $itm.parentList.Fields[$fieldname].ReadOnlyField = $false;
        $itm.parentList.Fields[$fieldname].update()

        $itm[$fieldname] = $sourceItm[$fieldname].toString()

        $itm.parentList.Fields[$fieldname].ReadOnlyField = $true;
        $itm.parentList.Fields[$fieldname].update()

    }
$itm["Author"] = $sourceItm["Author"].toString()
$itm["Editor"] = $sourceItm["Editor"].toString()
$itm.updateOverwriteVersions()
# Also tried $itm.update()
# Also tried $itm.systemUpdate()

Error:

Exception calling "SystemUpdate" with "0" argument(s): "The file Documents/1test.doc has
been modified by Domain\myusername on <date time stamp>."

I have tried to re get a handle on the item using

$itm = $itm.parentList.getItemById($itm.id)

I have tried to enable/disable unsafeupdates

$itm.parentList.parentWeb.AllowUnsafeUpdates = $true

Am I missing something here or going about this the wrong way? Please help!!

share|improve this question

2 Answers 2

recently I had similar error but with C# code. The reason was an event receiver set on the library.

When event receiver is set to library (item updated for example) and you try to update the item to avoid indefinite call of item update this exception is thrown.

Try to see if there is event receiver is attached to the library and if it is a case, you should edit event receiver temporary to disable event firing while you update is in progress using next Property of event receiver:

EventFiringEnabled = true|false;

Do not forget to renable event firing. If not - not events will be fired.

Hope it helps,

Andrew

share|improve this answer

My guess is that you are actually getting a missleading error, because the Modified and Created fields are automatically maintained by SharePoint and you should never try to update their values in any way.

In addition to that, you are actually doing 2 update operations, one to attempt to change the field Read-Only status and then you actually try to UpdateOverwrite the data. You should separate these 2 operations already, by retrieving once again a reference to the List before running the 2nd update (this, just in case where you want to try with other fields).

share|improve this answer
    
Probably another question for another form but how would I copy an item without changing the modified and created information? I will try your suggestion as well thanks. –  Realistic Feb 12 '13 at 23:01
    
Examples adicodes.com/… - you do not need to change these properties - SharePoint does it for you automatically. –  C. Marius - MVP Feb 12 '13 at 23:25
    
Examples look like exactly what I want. Only issue is this is 2007 not 2010 so I do not have a Copy-SPListData method. Is these a 2007 version? Also, when I said "without changing the modified and created information" I mean I want to copy this info with the item. That is, if the item I am copying was modified last by bob on jan 5, 2005 the new item in the target should also say it was modified by bob on jan 5, 2005. Just wanted to ensure we are on the same page. –  Realistic Feb 13 '13 at 0:03
    
The Copy-SPListData is actually a function defined below, on that same page, not SharePoint edition specific. To my knowledge you'l not be able to perfectly replicate as such, unless you rely on the COntent Deployment wizard, see here sharepoint.stackexchange.com/questions/33881/… –  C. Marius - MVP Feb 13 '13 at 8:02

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.