Configuring a sharepoint site subscriptions user account directory path with the object model
Asked Answered
M

1

7

I'm working on multi tenant provisioning in sharepoint and I'm having trouble figuring out if you can set the user account directory path for a site subscription using the sharepoint object model. I know this can be done through powershell with the following cmdlet.

    $sub = New-SPSiteSubscription 
    $sub | Set-SPSiteSubscriptionConfig -UserAccountDirectoryPath "OU=AlpineBikeStore,OU=Hosting,DC=contoso,DC=com" -FeaturePack "50976ac2-83bb-4110-946d-95b4b6e90d42" -Confirm:$false 

So far I've got the following code that will create a site subscription with a default site and feature pack. However, I can't figure out how to set the path to the users OU in active directory.

    //Create a default admin site for this tenant
    var site = new SPSite("https://contoso.com/", userToken);

    //Create the subscription and assign the default admin site to it.
    var sub = SPSiteSubscription.Create();
    sub.Add(site);

    //Get the feature pack and assign it to the subscription
    var featurePacks = SPSiteSubscriptionSettingsManager.Local.GetAllFeaturePacks();
    var pack = featurePacks.SingleOrDefault(x => x.Id == Guid.Parse("50976ac2-83bb-4110-946d-95b4b6e90d42"));
    SPSiteSubscriptionSettingsManager.Local.AssignFeaturePackToSiteSubscription(pack, sub);

Any suggestions?

Matildamatilde answered 17/7, 2012 at 19:3 Comment(1)
Did you try using reflection on the powershell commandlet to see what kind of code it uses?Kraus
S
1

As Rikard suggested I used reflection for you.

Set-SPSiteSubscriptionConfig does the following:

    if (this.m_UserAccountDirectoryPathSpecified)
    {
        SPSiteSubscriptionPropertyCollection adminProperties = this.m_SettingsManager.GetAdminProperties(this.m_ResolvedIdentity);
        if (!string.IsNullOrEmpty(this.UserAccountDirectoryPath))
        {
            adminProperties.SetValue("UserAccountDirectoryPath", this.UserAccountDirectoryPath);
        }
        else
        {
            adminProperties.Remove("UserAccountDirectoryPath");
        }
        adminProperties.Update();
    }

As you can see it uses the GetAdminProperties method to get the admin properties of the SPSiteSubscriptionManager. It then goes ahead and just updates the SPSiteSubscriptionProperty inside the adminProperties collection with the value "UserAccountDirectoryPath".

All you need to do now is to set this as well and you're done. You can use a program such as ILSpy to look at the code of the SharePoint Powershell commandlets. In this case you could have found the code in Microsoft.SharePoint.PowerShell.SPCmdletSetSiteSubscriptionConfig.

Skiagraph answered 20/11, 2012 at 8:11 Comment(2)
Thanks a bunch, we ended up needing to support both SP2010 and 2013 with our assembly so we decided to stick with the powershell cmdlets regardless. However, i'm sure that there are others that will get some use out of this. Thanks againMatildamatilde
Just to be on the safe side I checked SP2013 as well - same code. You'd be just as safe using this code for the new version :-)Skiagraph

© 2022 - 2024 — McMap. All rights reserved.