none
Change Owner, or SecondaryContact using PowerShell using the SPSite Class

    Question

  •  

    Hello All,

     

    Im hoping to manipulate the SecondaryContact stored in SPSite http://msdn.microsoft.com/en-us/library/ms978559(office.12).aspx using powershell the Data Type is "Microsoft.SharePoint.SPUser"

    http://msdn.microsoft.com/en-us/library/ms978595(office.12).aspx

     

    When i attempt to assign a user to the site.SecondaryContact i get the following error message.

     

     

    PS C:\> $foo = new-object Microsoft.Sharepoint.SPSite "http://server";
    PS C:\>
    PS C:\> $fooo = $foo.rootweb.site;
    PS C:\>
    PS C:\> $fooo.SecondaryContact = "domain\spadmin";
    Exception setting "SecondaryContact": "Cannot convert value "domain\spadmin" to type "Microsoft.SharePoint.SPUser".
     Error: "Invalid cast from 'System.String' to 'Microsoft.SharePoint.SPUser'.""
    At line:1 char:7
    + $fooo.S <<<< econdaryContact = "domain\spadmin";

    PS C:\>

     

    Anyone have any idea's what im doing wrong?

     

    Thanks

    ~Brian

     

    Tuesday, June 17, 2008 3:22 AM

Answers

  • Ok - to be more specific about how to get a user object:

    1. Get your site collection
        $sc = new-object Microsoft.SharePoint.SPSite("http://sitecollectionurl")

    2. Get a site that has the user you want to assign as Owner/SecondaryContact
          $site = $sc.OpenWeb("/path/to/site")

    3. Get the user by account name
            $user = $site.AllUsers.Item("DOMAIN\username");

    4.  Assign that user as the site collection Owner
        $sc.Owner = $user


    And you're done.

    • Proposed as answer by dunxd Tuesday, December 02, 2008 12:53 PM
    • Marked as answer by Michael Washam - MSFT Thursday, December 04, 2008 4:39 PM
    Tuesday, December 02, 2008 12:50 PM

All replies

  • You are assigning a string (ie. "domain\spadmin") where an object of the type SPUser is expected.

    What you need to do is get your required user from somewhere (I'm still figuring that one out - is it AD or the Sharepoint user profiles store?), store it in a variable, then assign that to your owner/secondary contact.

    Tuesday, December 02, 2008 11:58 AM
  • Ok - to be more specific about how to get a user object:

    1. Get your site collection
        $sc = new-object Microsoft.SharePoint.SPSite("http://sitecollectionurl")

    2. Get a site that has the user you want to assign as Owner/SecondaryContact
          $site = $sc.OpenWeb("/path/to/site")

    3. Get the user by account name
            $user = $site.AllUsers.Item("DOMAIN\username");

    4.  Assign that user as the site collection Owner
        $sc.Owner = $user


    And you're done.

    • Proposed as answer by dunxd Tuesday, December 02, 2008 12:53 PM
    • Marked as answer by Michael Washam - MSFT Thursday, December 04, 2008 4:39 PM
    Tuesday, December 02, 2008 12:50 PM