none
powershell error: you cannot call a method on a null-valued expression

    Question

  • Hi,

    I am not able to execute the PowerShell script from the C# code. It returns the error as "you cannot call a method on a null-valued expression". But I can run the same script from Sharepoint Management shell. Below is the part the code which throws error.

    #Creating RT_Site Owner permission level
      $spRoleDefinition = New-Object Microsoft.SharePoint.SPRoleDefinition
      $spRoleDefinition.Name = "RT – Site Owner"
      $spRoleDefinition.Description = "Cannot create subsites"
      $spRoleDefinition.BasePermissions = "ViewListItems, AddListItems,
      EditListItems, DeleteListItems,
      ApproveItems, OpenItems,
      ViewVersions, DeleteVersions, CreateAlerts, EditMyUserInfo,
      ManageLists, CancelCheckout, AddAndCustomizePages,
      AddDelPrivateWebParts, ApplyStyleSheets,
      ApplyThemeAndBorder, BrowseDirectories,
      BrowseUserInfo, EnumeratePermissions, ManageAlerts,
      ManagePersonalViews, ManageWeb,
      Open, UpdatePersonalWebParts,
      UseClientIntegration, UseRemoteAPIs,
      ViewFormPages, ViewPages, ViewUsageData"
      $spweb.RoleDefinitions.Add($spRoleDefinition)

    Please help me to fix the issue.

    Vijay

    Friday, August 24, 2012 12:33 PM

Answers

  • Seeing your powershell code runs without issue, directly in Sharepoint Management shell, then I do not see issues with the script. The problem may be on your C# code, that is calling and running the powershell script. Have you checked the MSDN forums, for a resolution there also? Not sure if there are any DLL's you may need to reference.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Tuesday, August 28, 2012 1:34 PM
  • Hi,

    Based on my understanding, it seems like that there are some thing we should load when we want to run the powershell script within the C# code, so I would like suggest you refer to the below link for the right forum:

    http://social.msdn.microsoft.com/Forums/en-US/categories

    Regards,

    Yan


    Yan Li

    TechNet Community Support

    Thursday, August 30, 2012 6:16 AM
    Moderator

All replies

  • IF it works in the Sharepoint Management Shell, but not anywhere else, it will most likely be you need to load the Sharepoint snapin?

    When you open Sharepoint Management shell, it automatically, loads the snapin so all cmdlets are readily available for your use. If you try to use the cmdlets in a normal powershell console, it will fail unless you load the snapin ahead of time.


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Friday, August 24, 2012 12:42 PM
  • Hi, Thanks for the reply.

    Yes, I am loading the SharePoint Snapin before executing the script. Below is the code.

    $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
    if ($snapin -eq $null) {
    #Write-Host "Loading SharePoint Powershell Snapin"
    Add-PSSnapin "Microsoft.SharePoint.Powershell"
    }

    Is there any special permission required to create the group?

    Friday, August 24, 2012 1:03 PM
  • Account must have permission to use Sharepoint powershell:

    Add-SPShellAdmin -UserName DOMAIN\user -database guid

    where guid is from Get-SPDatabace


    Сазонов Илья http://isazonov.wordpress.com/

    Friday, August 24, 2012 1:27 PM
  • Hi,

    I have included the spshelladmin cmdlet, but again the same "you cannot call a method on a null-valued expression".

    Add-SPShellAdmin -UserName domain\rtspsetup

    Friday, August 24, 2012 2:03 PM
  • Here are the instructions to configure permissions, in order to run powershell code for sharepoint

    SharePoint 2010 Products administration by using Windows PowerShell


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Friday, August 24, 2012 2:03 PM
  • Hi,

    Already the user is in SecurityAdmin and dbowner user group and also a local sys admin. But still the same error throws. Please suggest the alternate to overcome this issue.

    Saturday, August 25, 2012 5:28 AM
  • Please post the full error text, so we can see what method call is failing.

    Grant Ward, a.k.a. Bigteddy

    Saturday, August 25, 2012 2:15 PM
  • Hi,

    When I call the My PowerShell from the Management Shell its executing without any error. But, when I try to call the same file (powershell script file) from C#, it throws error. Error message is (you cannot call a method on a null-valued expression). I am not getting more than this error message.

    I have added the Sharepoint snapin in the script file. Is there any elevated privilages required to execute the PS script file from c#? If so, please let me know how to do that.

    Also, when I try to add the service account in the SPShallAdmin group it says "Cannot add domain\rtspsetup to the SharePoint_Shell_Access role of the database SharePoint_Config. A possible cause of this error is that the accoun
    t name was already added to the database as a login using a different user name than the account name." What will be the issue?

    Thanks,

    Vijay

    Saturday, August 25, 2012 4:57 PM
  • Hi,

    When I call the My PowerShell from the Management Shell its executing without any error.


    Under domain\rtspsetup?

    Сазонов Илья http://isazonov.wordpress.com/

    Sunday, August 26, 2012 6:29 AM
  • Yes its executing when I try to run the script from the Management Shell
    Monday, August 27, 2012 5:21 AM
  • Seeing your powershell code runs without issue, directly in Sharepoint Management shell, then I do not see issues with the script. The problem may be on your C# code, that is calling and running the powershell script. Have you checked the MSDN forums, for a resolution there also? Not sure if there are any DLL's you may need to reference.

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Tuesday, August 28, 2012 1:34 PM
  • Hi,

    Based on my understanding, it seems like that there are some thing we should load when we want to run the powershell script within the C# code, so I would like suggest you refer to the below link for the right forum:

    http://social.msdn.microsoft.com/Forums/en-US/categories

    Regards,

    Yan


    Yan Li

    TechNet Community Support

    Thursday, August 30, 2012 6:16 AM
    Moderator