0

What is the class available in SharePoint API to programmatically provision a new Managed Metadata service Application or How to provision a new Managed Metadata service application Programmatically using c#

1 Answer 1

1

The below code, from here, is the closest code I have seen to provision in C#, but it is missing crucial parts it feels:

private void ProvisionMetadataService()
    {
        // We don't have the metadata service configured, so let's do that.
        var proxy = SPFarm.Local.ServiceProxies.Where(s => s.GetType().Name.Equals("MetadataWebServiceProxy")).FirstOrDefault();
        if (null == proxy)
        throw new SPException("Failed to get instance of metadata web service proxy, is it installed?");
        foreach (var proxyApp in proxy.ApplicationProxies.Where(proxyApp =>
             proxyApp.Properties.ContainsKey("IsDefaultSiteCollectionTaxonomy")))
        {
            proxyApp.Properties["IsDefaultSiteCollectionTaxonomy"] = true;
            proxyApp.Update(true);
        }
        // Give the current user access rights to the metadata service.
        var service = SPFarm.Local.Services.Where(s => s.GetType().Name.Equals("MetadataWebService")).FirstOrDefault();
        if (null == service)
             throw new SPException("Failed to get instance of metadata web service, is it installed?");
        var serviceApp = service.Applications.OfType<SPIisWebServiceApplication>().FirstOrDefault();
        if (null == serviceApp)
             throw new SPException("Failed to get instance of metadata web service app, is it installed?");
        var security = serviceApp.GetAdministrationAccessControl();
        var cba = SPClaimProviderManager.Local;
        var claim = cba.ConvertIdentifierToClaim("DOMAIN\user",SPIdentifierTypes.WindowsSamAccountName);
        security.AddAccessRule(new SPAclAccessRule<SPCentralAdministrationRights>(claim, SPCentralAdministrationRights.FullControl));
        serviceApp.SetAdministrationAccessControl(security);
        serviceApp.Uncache();
        service.Uncache();
    }

I usually use PowerShell instead, code from here:

# Loading Microsoft.SharePoint.PowerShell
$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"
}

$ManagedAccount = Get-SPManagedAccount | select -First 1
if ($ManagedAccount -eq $null) { throw "No Managed Account" }

$ApplicationPool = Get-SPServiceApplicationPool "SharePoint Hosted Services" -ErrorAction SilentlyContinue
if ($ApplicationPool -eq $null)
{
   $ApplicationPool = New-SPServiceApplicationPool "SharePoint Hosted Services" -Account $ManagedAccount
   if (-not $?) { throw "Failed to create an application pool" }
}

Write-Progress "Creating Taxonomy Service Application" -Status "Please Wait..."

$MetadataseviceInstance = (Get-SPServiceInstance |?{$_.TypeName -eq "Managed Metadata Web Service"})
if (-not $?) { throw "Failed to find Metadata service instance" }

if ($MetadataseviceInstance.Status -eq "Disabled")
{
   $MetadataseviceInstance | Start-SPServiceInstance
   if (-not $?) { throw "Failed to start Metadata service instance" }
}

while (-not ($MetadataseviceInstance.Status -eq "Online"))
{
   Write-Host "Waiting for provisioning ..."; sleep 5;
}

$MetaDataServiceApp = New-SPMetadataServiceApplication -Name "Demo Metadata Service Application" -ApplicationPool $ApplicationPool
if (-not $?) {throw "Failed to create Metadata Service Application" }

$MetadataServiceAppProxy = New-SPMetadataServiceApplicationProxy -Name "Demo Metadata Service Application Proxy" -ServiceApplication $MetaDataServiceApp -DefaultProxyGroup

# This service application is the default storage location for Keywords.
$MetadataServiceAppProxy.Properties["IsDefaultKeywordTaxonomy"] = $false

# This service application is the default storage location for column specific term sets.
$MetadataServiceAppProxy.Properties["IsDefaultSiteCollectionTaxonomy"] = $false

# Consumes content types from the Content Type Gallery
$MetadataServiceAppProxy.Properties["IsNPContentTypeSyndicationEnabled"] = $false

# Push-down Content Type Publishing updates from the Content Type Gallery
# to sub-sites and lists using the content type.
$MetadataServiceAppProxy.Properties["IsContentTypePushdownEnabled"] = $false

$MetadataServiceAppProxy.Update()
9
  • We use New-SPMetadataServiceApplication in powershell..what is the nearest method in Sharepoint API if available??
    – kranthi
    Commented May 30, 2014 at 6:04
  • There is not, as I just wrote. The first snippet above is the closest for C#, and as you can see it is already counting on a proxy Commented May 30, 2014 at 6:06
  • It is counting on proxy, but what if the actual ManagedmetaData service is not set up?? I mean it is not yer configured. We have to provide an application Pool, Database server,Database Name and those are missing.
    – kranthi
    Commented May 30, 2014 at 6:09
  • Yepp, I do not think you can do that with C# Commented May 30, 2014 at 6:10
  • Creating an web application,we have the WebApplicationBuilderclass.., In the same way there should be a way to create service applications using Sharepoint APi
    – kranthi
    Commented May 30, 2014 at 6:11

Your Answer

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

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