Monday, February 16, 2015

Managed Metadata changes not applied to list items after changing the term Store Label

Problem:

Managed Metadata changes not applied to list items after changing the term Store Label. 

Sometimes you changed a term and you are wondering why the changed term cannot be seen in a List where you are using the terms in a column.

Consider the following scenario:
·   You are using Managed Metadata and administrate that within the central administration website.
·   You are using a List in a site and added a column to use Terms saved in the Managed Metadata database.
·   You changed a term in the taxonomy term store.
·   You may run manually the Taxonomy Update Scheduler job on the Scheduled Jobs central admin page or waited more than one hour; because the schedule of that job is set out of the box to run every hour.
·   You may see that the changed term has not been updated to the new value on the List where you are using those information.
·   You found the following entry in the ULS log:
·   Exception with ULS log entry:

Exception occurred while hidden list being updated: System.IO.FileNotFoundException: The site with the id 6c03a437-0d6d-44a3-a542-6235b854a36e could not be found.   
at Microsoft.SharePoint.SPSite..ctor(Guid id, SPFarm farm, SPUrlZone zone, SPUserToken userToken)   
at Microsoft.SharePoint.SPSite..ctor(Guid id)   
at Microsoft.SharePoint.Taxonomy.UpdateHiddenListJobDefinition.ProcessProxy(MetadataWebServiceApplicationProxy proxy)   
at Microsoft.SharePoint.Taxonomy.UpdateHiddenListJobDefinition.Execute(Guid targetInstanceId) 

Workaround:
To work around the issue and update the Taxonomy Hidden list manually you can use the following PowerShell script as follows.

Add-PSSnapin microsoft.sharepoint.powershell
$site=Get-SPSite <Site-URL>
[Microsoft.SharePoint.Taxonomy.TaxonomySession]::SyncHiddenList($site)
$site.dispose()

Thursday, February 12, 2015

Updating the User Profile Properties value by reading it from csv

Updating the User Profile Properties value by reading it from csv file:

The below script will read the username & update property from the csv file and update the user profile.

if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
  Add-PSSnapin Microsoft.SharePoint.PowerShell
}

$csvfile="c:\Temp\UserList.csv"
$mySiteUrl = "http://SiteURL"
$upAttribute = "Location"
$upUserName = "UserName"
$site = Get-SPSite $mySiteUrl
$context = Get-SPServiceContext $site
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context)
$csvData = Import-Csv $csvfile
foreach ($line in $csvData)
{
       if ($profileManager.UserExists($line.UserName))
       {
              $up = $profileManager.GetUserProfile($line.UserName)
              $up[$upUserName].Value + "|" + $up[$upAttribute].Value | out-file -filepath C:\temp\Before_UserUpdate.txt -append -width 200
              $up[$upAttribute].Value = $line.Location
              $up.Commit()
              $up[$upUserName].Value + "|" + $up[$upAttribute].Value | out-file -filepath C:\temp\After_UserUpdate.txt -append -width 200
             
        }
       else
       {
              $line.username | out-file -filepath C:\temp\UserNotFound.txt -append -width 200

        }
}

$site.Dispose()  

Thursday, February 5, 2015

Binding Term store/Term set in Dropdown control

Binding Term store/Term set in Dropdown control:


In .aspx.cs / .ascx.cs:

string siteURL = http://siteURL;

using (SPSite sSite = new SPSite(siteURL))
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPWeb web = sSite.OpenWeb())
{
TaxonomySession txnSession = new TaxonomySession(sSite);

string strTermStoreName = "Managed Metadata Service";
TermStore termstore = txnSession.TermStores[strTermStoreName];

Group group = termstore.Groups["GroupName"];

TermSet tsRegion = group.TermSets["TermSet name"];
DataTable dtMMSRegion = GetTermSet(tsRegion.Terms);

DataView dvMMSRegion = new DataView(dtMMSRegion);
dvMMSRegion.Sort = "Name ASC";

ddlLocation.DataSource = dvMMSRegion;
ddlLocation.DataTextField = "Name";
ddlLocation.DataValueField = "Name";
ddlLocation.DataBind();
}
});
}




        protected DataTable GetTermSet(TermCollection tc)
        {
            DataTable dtMMSTermTable = new DataTable();

            dtMMSTermTable.Columns.Add("Name", typeof(string));

            DataRow drMMSTermRow;

            foreach (Term t in tc)
            {
                drMMSTermRow = dtMMSTermTable.NewRow();
                drMMSTermRow[0] = t.Name;
                dtMMSTermTable.Rows.Add(drMMSTermRow);
            }

            return dtMMSTermTable;

        }

In .aspx / .ascx

<asp:DropDownList ID="ddlLocation" AutoPostBack="true" EnableViewState="true" runat="server"></asp:DropDownList>