2

I'm trying to use powershell to add a new item to a SharePoint list AND set the created by/author field. I don't have much experience with powershell but this seems fairly straightforward.

I have a 2013 environment and a list called Favorites that users populate with links they want to save. However, I'd like to be able to add a favorite for a user as read and edit access is limited to items created by the user.

My powershell script:

Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
$SPWeb = Get-SPWeb "http://sharepoint";
$SPList = $SPWeb.GetList("http://sharepoint/Lists/Favorites");
$userName = "i:0#.w|[domain]\[username]";
$user = Get-SPUser -web $SPWeb -Identity $userName;

$SPListItem = $SPList.Items.Add();
$SPListItem["URL"] = "http://www.cnn.com";
$SPListItem["Author"] = $user;
$SPListItem.Update();

However, this gives the error:

Exception calling "Update" with "0" argument(s): "Invalid data has been used to update    
the list item. The field you are trying 
to update may be read only."
At line:11 char:1
$SPListItem.Update();
~~~~~~~~~~~~~~~~~~~~
CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
FullyQualifiedErrorId : SPException

If the field has a read only property, I thought it would be possible to change that:

$SPList.Fields["Author"].ReadOnlyField = $false;

However, that gives me this error:

Property 'ReadOnlyField' cannot be found on this object; make sure it exists and is
settable.
At line:6 char:1
$SPList.Fields["Author"].ReadOnlyField = $false;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CategoryInfo          : InvalidOperation: (:) [], RuntimeException
FullyQualifiedErrorId : PropertyNotFound

Edit: after further investigation, there were recent security updates made to our SharePoint server by our network admin that may be related to this script no longer working. I will try to update this post with what I find out.

Any ideas are still appreciated.

Edit 2: see below for the solution I used.

1

3 Answers 3

5

Below is what I needed to change to get this to work:

Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
$SPWeb = Get-SPWeb "http://sharepoint";
$SPList = $SPWeb.GetList("http://sharepoint/Lists/Favorites");
$userName = "i:0#.w|[domain]\[username]";
$user = Get-SPUser -web $SPWeb -Identity $userName;
$userString = "{0};#{1}" -f $user.ID, $user.UserLogin.Tostring()

$SPListItem = $SPList.Items.Add();
$SPListItem["URL"] = "http://www.cnn.com";
$SPListItem["Author"] = $userString;
$SPListItem.UpdateOverwriteVersion();

$userString is the correct format for the user and using UpdateOverweiteVersion() instead of update() saved the changes to the Author field.

3

Please, refer to this link to modify this field:

$SPSite = New-Object Microsoft.SharePoint.SPSite("http://sharepoint")
$SPWeb = $SPSite.OpenWeb()
$SPFieldUserValue = New-Object Microsoft.SharePoint.SPFieldUserValue($SPWeb,8, "#Test R.Test") 
$ListItem["Author"] = $SPFieldUserValue                   
$ListItem.Update() 
$SPWeb.Update()

As you can see, you must create a SPFieldUserValue as mentionned above. The number '8' is the user ID (you get it from Get-SPUser I guess -cannot test it right now-, or please check SPWeb users -$SPWeb.users ?-).

Other point, please check the $SPWeb.AllowUnsafeUpdates value. Must be to trueotherwise you won't be able to modify the ReadOnly property. According to https://sharepoint.stackexchange.com/a/100557/17814

1
  • 1
    You don't need to create a new object for that. You can just do: $user = $web.EnsureUser("Domain\User")
    – Bunzab
    Feb 4, 2015 at 15:15
-2

Try adding an update right after creating the record....

$SPListItem = $SPList.Items.Add();
$SPListItem.Update();
$SPListItem["URL"] = "http://www.cnn.com";
$SPListItem["Author"] = $user;
$SPListItem.Update();
2
  • Thanks for your quick response, but that still gives the first error message above.
    – ams
    Feb 3, 2015 at 20:37
  • You may need to create the items first...and then loop through and update the values. See here: gallery.technet.microsoft.com/projectserver/…
    – jpollar
    Feb 3, 2015 at 20:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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