Subscribe: SharePoint SharePoint SharePoint

Labels

Showing posts with label SharePoint 2010. Show all posts
Showing posts with label SharePoint 2010. Show all posts

Thursday, February 6, 2014

How to provision Managed Metadata columns in SharePoint

Once i came across a requirement from my client involving creating of a branding package for his SharePoint site in visual studio. Now in this particular package i had to add custom master pages, page layouts, css files, js files etc.

For each page layout i had to create separate content types with some site columns. These site columns too were created in visual studio.

Now one of these site columns was a metadata column i.e. a "Taxonomy Field". I did create the whole package but when i deployed it, i found that metadata columns were not connected to Term Store.

Powershell


Since this was the first time i had worked on this kind of project so was not aware that MMS Term store needs to be connected to a Taxonomy column. I could have done the same using UI, but my client wanted it to be automated.

I came across two approaches to do so. So thought of sharing those:

1. Elements.xml : In elements.xml file of the site column, we can easily specify the termsetid so that the column can connect to term set:

*Here ShowField="Term1033" is necessary field property.
NOTE: You can find the SSPID, GroupId and TermSetID from Term Store Management link in Site Settings.
 <?xml version="1.0" encoding="utf-8"?>  
 <Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
  <Field  
     ID="{99e8cfdc-3edb-4d0e-b22e-0bde0cfe8dfd}"  
     Type="TaxonomyFieldType"  
      DisplayName="LOB"  
      ShowField="Term1033"  
      EnforceUniqueValues="FALSE"  
      Group="Custom"  
      StaticName="LOB"  
      Name="LOB">  
   <Customization>  
    <ArrayOfProperty>  
     <Property>  
      <Name>SspId</Name>  
      <Value  
      xmlns:q1="http://www.w3.org/2001/XMLSchema"  
      p4:type="q4:string"  
      xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">  
       9a5c42de-abb1-489e-87df-c68c2a30c9fc  
      </Value>  
     </Property>  
     <Property>  
      <Name>GroupId</Name>  
      <Value  
      xmlns:q2="http://www.w3.org/2001/XMLSchema"  
      p4:type="q2:string"  
      xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">  
       d86f5d20-8878-4b0d-8ef2-b7a24272e5f3  
      </Value>  
     </Property>  
     <Property>  
      <Name>TermSetId</Name>  
      <Value  
      xmlns:q2="http://www.w3.org/2001/XMLSchema"  
      p4:type="q2:string"  
      xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">  
       3ff7c119-8ad1-4676-b4f0-6d5618840527  
      </Value>  
     </Property>  
     <Property>  
      <Name>AnchorId</Name>  
      <Value  
      xmlns:q3="http://www.w3.org/2001/XMLSchema"  
      p4:type="q3:string"  
      xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">  
       00000000-0000-0000-0000-000000000000  
      </Value>  
     </Property>  
    </ArrayOfProperty>  
   </Customization>  
  </Field>  
 </Elements>  

2. Powershell : If we have already deployed the solution without connecting the columns to term store and we don't not want to change anything in the package. Then we can utilize powershell to connect to the Term Store :

 $centralAdmin = Get-SPWebApplication -IncludeCentralAdministration | Where {$_.IsAdministrationWebApplication} | Get-SPSite  
 $session = new-object Microsoft.SharePoint.Taxonomy.TaxonomySession($centralAdmin)  
 $serviceApp = Get-SPServiceApplication | Where {$_.TypeName -like "*Metadata*"}  
 $termStore = $session.TermStores[$serviceApp.Name]  
 $termSet = $termStore.Groups["Department"].TermSets["IT"]  
 $site = Get-SPSite http://myWebApp/sites/mySite  
 $web = $site.RootWeb  
 $taxonomyField = $web.Fields | Where { $_.Id -eq "99e8cfdc-3edb-4d0e-b22e-0bde0cfe8dfd" }  
 $taxonomyField.SspId = $termSet.TermStore.Id  
 $taxonomyField.TermSetId = $termSet.Id  
 $taxonomyField.AllowMultipleValues = $false  
 $taxonomyField.Update();  

Make sure you specify the GUID of the site column correctly, so that it can connect to the term store.

After following any of the above approaches you can see that the column is connected to Term Store.:

Powershell

Thursday, January 30, 2014

Web Part cannot be imported.It's is not registered as safe

While trying to add a custom web part in SharePoint we might come across an error like :

"A Web Part or Web Form Control on this Page cannot be displayed or imported. The type could not be found or it is not registered as safe"

SharePoint 2013



This happens when safe control entries are set to false.

This behavior can be corrected in two ways:

1. Adding safe entries in web.config file :

  a. Open web.config
  b. Locate the SafeControls Tag
  c. Make a safe control entry

    <SafeControl Assembly="Assembly name, Version=1.0.0.0, Culture=neutral,    PublicKeyToken=d8eb6481d8b4beec" Namespace="your webpart namespace" TypeName="*" Safe="True" />

2. Adding safe entries  in SharePointProjectItem.spdata file which can be found in your project    folder: Project Folder-> Module ->spdata file.

 <SafeControl Assembly="Assembly name, Version=1.0.0.0, Culture=neutral,    PublicKeyToken=d8eb6481d8b4beec" Namespace="your webpart namespace" TypeName="*" Safe="True"  />

In some cases the issue is resolved by following any one of them while in other cases both of these methods need to be implemented.

Wednesday, January 29, 2014

Migrating Managed Metadata Term Stores in SharePoint Server across Farms

While moving a site from development environment to production environment, it is recommended to move MMS term stores too. We can actually adopt two approaches to this:

1. Export Term Stores in a .csv file and then Import them to target server.
2. Use a powershell script to create a .cab file to import on target server.

SharePoint

The first approach is easier but has a major drawback. It does not persist the GUID of the terms. This may lead to many errors in the imported site because some web parts might be dependent on a term's guid.

e.g. if i have a CQWP which shows filtered data as per a MMS term, then in the target server the filtering would be lost.

So a better and safe way is to export and import MMS using Powershell:

Export

SharePoint

 Add-PSSnapin Microsoft.SharePoint.Powershell
 $metadataApp= Get-SpServiceApplication | ? {$_.TypeName -eq "Managed Metadata Service"}
 $mmsAppId = $metadataApp.Id

 $mmsproxy = Get-SPServiceApplicationProxy | ?{$_.TypeName -eq "Managed Metadata Service Connection"}
 Export-SPMetadataWebServicePartitionData -Identity $mmsAppId -ServiceProxy $mmsproxy -Path "C:\Users\Desktop\MMD.cab”

Import


SharePoint

Add-PSSnapin Microsoft.SharePoint.Powershell
 $metadataApp= Get-SpServiceApplication | ? {$_.TypeName -eq "Managed Metadata Service"}
 $mmsAppId = $metadataApp.Id

 $mmsproxy = Get-SPServiceApplicationProxy | ?{$_.TypeName -eq "Managed Metadata Service Connection"}

Import-SPMetadataWebServicePartitionData -Identity $mmsAppId -ServiceProxy $mmsproxy -Path "//SharedPath/Users/Desktop/MMD.cab" -OverwriteExisting;

NOTE: While importing make sure the .cab file is in a network path accessible to SQL Server with ContentDB and which has been shared with the user account of the MMS Service.

Saturday, July 6, 2013

Hiding Quick Launch in SharePoint

Hiding the quick launch in SharePoint can be done in a number of different ways but the easiest way is by overriding the current css class implemented by "Quick Launch"  in Content Editor WebPart (CEWP) :


Paste the following code in a CEWP and it will hide the quick launch bar :
SP2010
#s4-leftpanel{
display:none
}
.s4-ca{
margin-left:0px
}

SP2013
.ms-core-navigation { DISPLAY: none }
#contentBox { margin-left: 0px }

Delete a list in sharepoint 2010 using powershell

Deleting a list through UI is sometimes a hectic job when your server is slow, so a better approach is to delete the list using powershell. This method is much faster and reliable :

$site = Get-SPSite <Site Collection Name>
$web= $site.openweb()
$lists = $web.Lists
$getList = $web.lists["ListName"]
$getList.Delete()

NOTE : Replace <Site Collection Name> with your current Site Collection Name.
            : You should have required access over ContentDB to delete the list using powershell.

How to get current user displayname with javascript in sharepoint ?

If you need to retrieve the name of the current logged in user in sharepoint , it can be easily done with the help of a ECMA script. Following code illustrates the same : 

<script type=”text/javascript”>
ExecuteOrDelayUntilScriptLoaded(getDisplayName,”sp.js”);
var currentUser;
function getDisplayName(){
this.clientContext = new SP.ClientContext.get_current();
this.oWeb = clientContext.get_web();
currentUser = this.oWeb.get_currentUser();
this.clientContext.load(currentUser);
this.clientContext.executeQueryAsync(Function.createDelegate(this,this.onQuerySucceeded), Function.createDelegate(this,this.onQueryFailed));
}

function onQuerySucceeded() {
alert( currentUser.get_loginName());
}

function onQueryFailed(sender, args) {
alert(‘Request failed. \nError: ‘ + args.get_message() + ‘\nStackTrace: ‘ + args.get_stackTrace());
}
< /script>

Friday, July 5, 2013

Content Editor Web Part script error

Many a times when we add a ECMA script to a CEWP, it does not work as expected. This mainly happens because CEWP does not start a server thread. ECMA script can only be executed when a server thread or worker process is associated to it. So in order to make it work, we need to add a function i.e. "ExecuteOrDelayUntilScriptLoaded". It first loads the "SP.js" file so that it can start a server thread.

SharePoint 2010


After loading the "SP.js" file , the ECMA script in the CEWP is executed without any error.

Following is the code depicting the same :

Saturday, June 22, 2013

Internal Names in site columns in SharePoint

While creating a custom list in SharePoint 2010 or 2013, we usually retain the "Title" column while create all the other columns manually. Now while programmatically using the column we specify "Tilte" as column name even if we have changed the name to something else.

e.g. if we have changed the "Tilte" column name to "EmpName" , then in ECMA script we will use :

listItem.set_item('Title', 'My New Item!');

"Title" is  a site column which has been predefined in the site. Similarly there are a large number of columns in site.

Each site column has two attributes i.e "Name" and "Display Name". "Name" refers to the internal name or the field while "Display Name"  is the name which is displayed in the UI. Now if the "Display Name"  is "Employee Name" then the "Name"  is mapped as "Employee_x0020_ Name"  because space is interpreted as '_x0020' .