Wednesday 8 April 2015

Getting managed metadata from a another system. I recently needed to extract the managed metadata terms from a server to which I had very little access, from a computer that wouldn't allow me to run PowerShell scripts. I did so using a Windows Forms application instead. I thought I'd post the code here for future benefit:

public void GetTerms(ClientContext context, StreamWriter file, Term term, string prefix)
{
    TermCollection subTerms = term.Terms;
    context.Load(subTerms);
    context.ExecuteQuery();
    foreach (Term subTerm in subTerms)
    {
        file.WriteLine(prefix + subTerm.Name + "," + subTerm.Id);
        GetTerms(context, file, subTerm, prefix + subTerm.Name + "/");
    }
}

private void button1_Click(object sender, EventArgs e)
{
    //Recursive function to get terms


    //Specify admin user and SharePoint site URL
    //User = "e-sjones"
    string site = "mysite";


    //Pwd = Read-Host -Prompt "Enter your password" -AsSecureString
    ClientContext context = new Microsoft.SharePoint.Client.ClientContext(site);
    Web web = context.Web;
    context.Load(web);
    context.ExecuteQuery();
    //Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials(User,Pwd)
    //Context.Credentials = Credentials
    TaxonomySession mms = TaxonomySession.GetTaxonomySession(context);
    context.Load(mms);
    //Context.ExecuteQuery()

    //Get Term Stores
    var termStores = mms.TermStores;
    context.Load(termStores);
    context.ExecuteQuery();
    var termStore = termStores[0];
    context.Load(termStore);
    context.ExecuteQuery();

    //Get Groups
    var Groups = termStore.Groups;
    context.Load(Groups);
    context.ExecuteQuery();

    //Create the file and add headings
    var outputFile = @"c:\temp\Output File Path.txt";

    var file = new StreamWriter(outputFile);
    file.WriteLine("Term Set Name,GUID");

    foreach (TermGroup group in Groups)
    {
        context.Load(group);
        context.ExecuteQuery();
        file.WriteLine();
        file.WriteLine("-----------");
        file.WriteLine();
        file.WriteLine(group.Name + "," + group.Id);
        var TermSets = group.TermSets;
        context.Load(TermSets);
        context.ExecuteQuery();
        foreach (TermSet termSet in TermSets)
        {
            file.WriteLine("--");
            file.WriteLine(termSet.Name + "," + termSet.Id);

            var Terms = termSet.Terms;
            context.Load(Terms);
            context.ExecuteQuery();
            foreach (Term term in Terms)
            {
                file.WriteLine(term.Name + "," + term.Id);
                GetTerms(context, file, term, term.Name + "/");
            }
        }
    }
    file.Flush();
    file.Close();
}

I can't really claim credit by the way. This was a re-write of a PowerShell script that I found here: http://sharepointrapt.blogspot.co.uk/2014/08/export-sharepoint-managed-metadata.html


Friday 7 September 2012

Brian Murphy's DelegConfig tool

Normally when I'm doing deployments, at some point Kerberos will turn around and bite me in the arse. Every time I think I'm getting the hang of this three-headed dog from hell, it surprises me again. The tool that's normally got me out of this hole is Brian Murphy's DelegConfig tool. Here's his blog site about it:

http://blogs.iis.net/brian-murphy-booth/archive/2007/03/09/delegconfig-delegation-configuration-reporting-tool.aspx

Anyway, recently Microsoft have rejigged their iis.net website and the download link on the blog page no longer works. Here is the new link:

http://www.iis.net/downloads/community/2009/06/delegconfig-v2-beta-(delegation-kerberos-configuration-tool)

Thursday 9 August 2012

SharePoint 2010 Creation of Managed Properties

This is something that's always really bugged me. There's no good way to set these up in one environment, then port them to Test and Production environments.

Since we're getting more and more into PowerShell scripting, I figured there must be a PowerShell method of doing this. Sure enough, Corey Ross has written a great article about it here:

http://dotnet.sys-con.com/node/1410497

Here's the final code for reference:


$searchapp = Get-SPEnterpriseSearchServiceApplication "Search Service Application"
$category = Get-SPEnterpriseSearchMetadataCategory –Identity SharePoint -SearchApplication $searchapp
$crawledproperty = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category $category -VariantType 31 -PropSet "00130329-0000-0130-c000-000000131346" -Name ows_TestProperty1 -IsNameEnum $false
$managedproperty = New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Name TestProperty1 -Type 1
New-SPEnterpriseSearchMetadataMapping -SearchApplication $searchapp -ManagedProperty $managedproperty -CrawledProperty $crawledproperty

$crawledproperty = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category $category -VariantType 31 -PropSet "00130329-0000-0130-c000-000000131346" -Name ows_TestProperty2 -IsNameEnum $false
$managedproperty = New-SPEnterpriseSearchMetadataManagedProperty -SearchApplication $searchapp -Name TestProperty2 -Type 1
New-SPEnterpriseSearchMetadataMapping -SearchApplication $searchapp -ManagedProperty $managedproperty -CrawledProperty $crawledproperty
$crawledproperty = New-SPEnterpriseSearchMetadataCrawledProperty -SearchApplication $searchapp -Category $category -VariantType 31 -PropSet "00130329-0000-0130-c000-000000131346" -Name ows_TestProperty2a -IsNameEnum $false
New-SPEnterpriseSearchMetadataMapping -SearchApplication $searchapp -ManagedProperty $managedproperty -CrawledProperty $crawledproperty

Monday 6 August 2012

Copying Managed Metadata from one farm to another


I was faced with the task of copying managed metadata values from a Test farm to a Production farm. Now I've always deployed Managed Metadata values by using a script before, so I could just deploy it on each farm, but this time I'm taking over someone else's project. My last attempt to google methods of moving Managed Metadata when moving from Dev to Test resulted in failure. I found a tool on Codeplex that purported to do it, but I couldn't make it work. Having already typed all of the metadata in twice (along with synonyms) already, the idea of typing them all in again filled me with dread, especially when moving to production. I debated writing my own tool, but it was half past 10 at night and it needed to be done by the morning. Frankly it would probably take less time to retype them.

Luckily my renewed search attempt was more fruitful and resulted in the following article: http://morshemesh.blogspot.co.uk/2011/12/transferring-managed-metadata-between.html. This was perfect, thank you Mor Shemesh.

... Perfect, except it didn't work. The export PowerShell script ran, but failed to create the backup file. In fact it didn't provide any messages at all (I didn't check the ULS logs). Brilliant. I checked it through and yes, the Proxy was returned correctly, the Service App was returned and the App ID looked correct as well.

It occurred to me at this point that I was running it on the WFE and the Managed Metadata service application was on the App server, so I ran it there. Success, I have a backup file.

So the next step is to run it on the production farm. I logged on to the App server this time rather than the WFE (won't catch me making the same mistake twice), ran the import command and...

You do not have permission to use the bulk load statement

Ok, what does this mean?

Well, googling the error message mostly finds references to SQL Server and tells you that you require the Bulkadmin privilege in SQL Server. Ok fine, I logged in to SQL server and gave myself the Bulkadmin privilege. Still no joy.

It turns out that it's the service application account that requires the privilege. So log on to SharePoint Central Administration and under Security select Configure Service accounts. Select your Managed Metadata service application to find out what account it's running under. Having given this account Bulkadmin privilege in SQL Server and re-run the command, I now have this error:

Import-SPMetadataWebServicePartitionData : Cannot bulk load because the file "C:\ 79A76FF111B34C67BA4E755019FC8347 \ECMGroup.dat" could not be opened.

Ok, this is quite weird, but this page here gave me the answer: http://social.technet.microsoft.com/wiki/contents/articles/5233.export-import-termset-en-us.aspx. Basically I created a network share on the SQL server and gave Everyone read/write access to it. I copied the Managed Metadata backup here and used the UNC path to the file in the PowerShell script. Success at last!

Now I just had to remap my Managed Metadata site columns to point to these newly added ones and was all done by half 11. Time to get some sleep!

Thursday 2 August 2012

Useful Powershell script to wait until solutions are deployed or retracted

After at last deciding to sort myself out with some Powershell scripts for automatically deploying or retracting solutions, I came across this one. It will wait for any scheduled jobs to complete on a WSP. I found it on Brian Cartmel's blog: http://sharepintblog.com/2011/06/13/wait-for-a-solution-to-deploy-with-powershell/

For reference, here's the script again:


## Get the solution 
$Solution = Get-SPSolution -identity $SolutionFileName
$lastStatus = ""
## loop whilst there is a Job attached to the Solution 
while ($Solution.JobExists -eq $True) 

    $currentStatus = $Solution.JobStatus 
    if ($currentStatus -ne $lastStatus) 
    { 
            Write-Host "$currentStatus…" -foreground green -nonewline 
            $lastStatus = $currentStatus 
    } 
    Write-Host "." -foreground green -nonewline 
    sleep 1 

## completed 
Write-Host "" 
Write-Host "     " $Solution.LastOperationDetails -foreground green