2

From the server itself this works:

Add-PSSnapin "Microsoft.SharePoint.PowerShell"


$SPWeb = Get-SPWeb http://mysharepoint.org
$SPList = $SPWeb.Lists["My List"]
$SPListItem = $SPList.GetItemByUniqueId("<my unique id>")
$SPListItem["Name"] = "stuff"
$SPListItem["Title"] = "stuff"
$SPListItem.Update()

But remotely doing this:

$s = New-PSSession -ComputerName mysharepoint.org
Invoke-Command -Session $s {
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
$SPWeb = Get-SPWeb http://mysharepoint.org
$SPList = $SPWeb.Lists["My List"]
$SPListItem = $SPList.GetItemByUniqueId("<my unique id>")
$SPListItem["Name"] = "stuff"
$SPListItem["Title"] = "stuff"
$SPListItem.Update()
}

Gets me:

You cannot call a method on a null-valued expression. + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull + PSComputerName : mysharepoint.org

Cannot index into a null array. + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : NullArray + PSComputerName : mysharepoint.org

Cannot index into a null array. + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : NullArray + PSComputerName : mysharepoint.org

You cannot call a method on a null-valued expression. + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull + PSComputerName : mysharepoint.org

Now clearly this is because I am getting a null list returned. But I can't understand why. Powershell Remoting is something I am just getting in to. How can I remotely update a list via powershell?

  • Have you gone through these steps? blogs.technet.microsoft.com/anneste/2014/09/17/… – Ransher Singh Jun 8 '16 at 17:38
  • I am a domain administrator so I felt like these steps would not apply. If that is not the case then I can. – workinonit530 Jun 8 '16 at 17:42
  • Maybe not, but try all the steps that need to be executed on the client side of that article and make sure you can connect – Ransher Singh Jun 8 '16 at 18:04
  • I guess I should have clarified. I am able to do this: $s = New-PSSession -ComputerName mysharepoint.org Invoke-Command -Session $s {$SPWeb = Get-SPWeb mysharepoint.org} – workinonit530 Jun 8 '16 at 18:08
  • Can you get a reference to the list if you try to get it using a method instead of indexer to the colletion? I.E. by trying $SPWeb.Lists.TryGetList('List Name')? – Dylan Cristy Jun 8 '16 at 19:22
3

I finally found the answer!

adding these two lines to the top of my script worked.

$w = Get-SPWebApplication -Identity https://mysharepoint.org
$w.GrantAccessToProcessIdentity("domain\my_username")

Full script:

$s = New-PSSession -ComputerName mysharepoint.org
Invoke-Command -Session $s {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"

    $w = Get-SPWebApplication -Identity https://mysharepoint.org
    $w.GrantAccessToProcessIdentity("domain\my_username")

    $SPWeb = Get-SPWeb http://mysharepoint.org
    $SPList = $SPWeb.Lists["My List"]
    $SPListItem = $SPList.GetItemByUniqueId("<my unique id>")
    $SPListItem["Name"] = "stuff"
    $SPListItem["Title"] = "stuff"
    $SPListItem.Update()
}
0

If the stand alone web application and SharePoint are not in the same domain, the simple answer is that you cannot update an item in SharePoint list unless you are using Apps.

The more complex answer is that you would need to have a proxy of some sort to do this. You could write a simple layer 7 proxy inside the non-SharePoint web app that forwards the requests and the authentication or use something like Squid or even IIS itself to do this.

If both applications are in the same network and wouldn't need to make calls across the firewall. than yes it can be done via REST API

Another consideration might be hosting the non-SharePoint site in the same domain as SharePoint.

You can update the list item using REST API. Please find the answer here.

SharePoint 2013 REST api to update list item

SharePoint 2013 – CRUD on List Items Using REST Services & jQuery

UPDATED

Update list item example using powershell

$SPAssignment = Start-SPAssignment
$SPWeb = Get-SPWeb http://SP -AssignmentCollection $spAssignment

# Next step is to get the list:

$SPList = $SPWeb.Lists["Announcements"]

# When we have located the list we can retrieve the item. The quickest way is to use the GetItemByID() method:

$SPItem = $SPList.GetItemById("1")

# The example above requires that you know the ID of the item. If you don’t know the items ID you can use the Where-Object cmdlet instead:

$SPItem = $SPList.Items | Where { $_["Title"] -eq "New Announcement" }

# When you’ve retrieved the item you can modify the item information.

$SPItem["Title"] = "MyTitle"
$SPItem["Body"] = "MyBody"

# After modifying an item you have to call the Update() method to set the changes.

$SPItem.Update()

# When you’re done, use the Stop-SPAssignment cmdlet to dispose the SPWeb object.

Stop-SPAssignment $SPAssignment
  • I am not writing an application, just a simple script that I would like to be able to execute from my computer (in the same domain as the Sharepoint server) to update a list item when I login. – workinonit530 Jun 8 '16 at 18:57
  • Please check updated answer – Hardik Jun 8 '16 at 19:10
  • @Hardik I think you have mis-understood the OP's question. They can update a list item using Powershell as in your example, when they are executing the Powershell on the SharePoint server. The question is about updating a list item through a remote Powershell session. – Dylan Cristy Jun 8 '16 at 19:20
  • @DylanCristy Exactly! I can do the updating on the server. I'd like to do it remotely. – workinonit530 Jun 8 '16 at 19:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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