Subscribe: SharePoint SharePoint SharePoint

Labels

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

Wednesday, February 5, 2014

"Sign in as Different User" menu option is missing in SharePoint 2013

In SharePoint 2013 you might have noticed that "Sign in as different User" menu option has been removed. Microsoft has decided to discontinue this option owing to a variety of different reasons. Though to SharePoint Admins and Approvers it might seem to be a big issue.

Since in SharePoint 2010, this menu option did offer a lot of help in checking the permission levels for various groups in SharePoint sites, same thing does not hold true for SharePoint 2013.



The reasons which might have caused Microsoft to remove this option may be summarized as below:

1. There are a lot of caching problems. The cache for one user can contradict with the other one.
2. Page content might show data and content from previous logged in users.

Now there are a number of ways to use this option :

1. In the browser just  append this link to your site url : /_layouts/closeConnection.aspx?loginasanotheruser=true

2. Another permanent solution is to make changes to the welcome.ascx user control which is located at "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\CONTROLTEMPLATES\welcome.ascx"

Add the following code in it before this tag --> "SharePoint:MenuItemTemplate tag with id ID_RequestAccess":

 <SharePoint:MenuItemTemplate runat="server" ID="ID_LoginAsDifferentUser"  
 Text="<%$Resources:wss,personalactions_loginasdifferentuser%>"   
 Description="<%$Resources:wss,personalactions_loginasdifferentuserdescription%>"   
 MenuGroupId="100"  Sequence="100"  UseShortId="true"  />  

After doing this do an IISREST and you should be able to see the required option :




Tuesday, February 4, 2014

Check in and approve all publishing pages using PowerShell in SharePoint

We have a SharePoint 2013 farm setup for our company Intranet. There are huge number of sites in the intranet. Each site is maintained by a separate group. Now the people who maintain those sites are non-technical people.

Since all the sites are publishing sites, so the pages need to be published everytime they are edited. I have repeatedly sent mails to all groups stating that this procedure has to followed everytime they make changes.

But alas! , everyday my mailbox is clogged with requests reporting that "changes are not being reflected to site visitors". Instead of sending separate mails to each of them, i made a powershell script to publish all their pages together.

Note: The image is just for reference. Its not actual screenshot of the below code.

I actually used a timer service to run this script everyday at 5 AM. Finally after few days, i started getting very few requests. So thought of sharing the script:

 $listname = "Pages"   
 $url = "http://myWebApp/sites/mySite"  
    
 function approvePublishContent ($w, $listName) {  
  $list = $w.Lists |? {$_.Title -eq $listName}  
  foreach ($item in $list.Items)   
  {
   //Checking if item is locked
   if(($item -ne $null) -and ($item.LockId -ne $null)) {  
    $item.ReleaseLock($item.LockId)  
   }  
   if( $item.File -ne $null) { $itemFile = $list.GetItemById($item.ID).File }  
   else { $itemFile = $list.GetItemById($item.ID) }  
   
   //Checking in the item
   if( $itemFile.CheckOutStatus -ne "None" ) {   
    $itemFile.CheckIn("Automatic CheckIn. (SysAdmin)")  
    if( $item.File -ne $null) { $itemFile = $list.GetItemById($item.ID).File }  
    else { $itemFile = $list.GetItemById($item.ID) }  
   }  

   //Publishing the item
   if( $list.EnableVersioning -and $list.EnableMinorVersions) {   
    $itemFile.Publish("Automatic Publish. (SysAdmin)")  
    if( $item.File -ne $null) { $itemFile = $list.GetItemById($item.ID).File }  
    else { $itemFile = $list.GetItemById($item.ID) }  
   }  

   //Approving it in case approval is required
   if( $list.EnableModeration ) {   
    $itemFile.Approve("Automatic Approve. (SysAdmin)")   
   }  
  }  
 }  
 $site = Get-SPSite $url  
 foreach ( $web in $site.AllWebs )  
 {  
  approvePublishContent $web $listname  
 }  
 Write-Output "OK"  
   

Monday, February 3, 2014

Updating Web part properties using PowerShell in SharePoint

Web part properties define the structure as well as functionality of a web part. They are quite useful while configuring a web part. We have a huge list of web part properties which have been defined by microsoft. you can have a look over these properties from the link below:

SharePoint Web part properties

Now once i ran into a situation wherein i had to change the "ChromeType" property of a web part in the landing page of all the subsites as well as root site in my site collection. There were a total of 32 subsites. So in order to do this through User Interface would have required a considerable amount of time.
SharePoint 2010

So i created a powershell script to do so and voila! I was able to do so in a single shot.

Each web part page is associated with a "SPWebPartManager" class. This class in particular contains references to all the web parts on the page. So in order to access my web part i have to iterate through the "SPWebPartManager" class.

Here the web part name is "Relevant Articles" and the chrome property has to be set to "TitleAndBorder".
  
 //Get reference to the SPSite object
 $SPsite = Get-SPSite "http://mywebapp/sites/mySite"  
   
 //Iterate through all sub sites and root site  
 foreach($SPWeb in $SPsite.AllWebs){  
   
 //Get reference to the landing page. Since it is a publishing site so default.aspx is the landing page  
 $page = $SPWeb.GetFile("Pages/default.aspx")  
   
 //Checkout the page  
 $page.CheckOut()  
   
 //Get reference to the webpartmanager class  
 $webpartmanager = $SPWeb.GetLimitedWebPartManager("Pages/default.aspx",   
 [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)    
   
   
 //Iterate through webparts in webpartmanager class  
  for($i=0;$i -lt $webpartmanager.WebParts.Count;$i++)  {   
   
 //Check for the name of required web part   
  if($webpartmanager.WebParts[$i].title -eq "Relevant Articles")   
  {    
   
     //Get reference to the web part  
     $wp=$webpartmanager.WebParts[$i];  
       
     //Set the chrome property  
     $wp.ChromeType="TitleAndBorder";  
   
     //Save changes to webpartmanager. This step is necessary. Otherwise changes won't be reflected  
     $webpartmanager.SaveChanges($wp);  
       
     break;   
   
  }   
 }   
   
  //Check in and Publish the page  
  $page.CheckIn("Relevant Articles")  
  $page.Publish("Relevant Articles")  
   
  // Update the SPWeb object  
  $SPWeb.Update();   
    
  //Dispose SPWeb object  
  $SPWeb.Dispose();  
 }  

Sunday, February 2, 2014

New Delegate Controls in SharePoint 2013

Delegate Controls are very significant medium to make changes to master page without touching the code part. In SharePoint 2010, we had a lot of delegate controls. To name a few we had : AdditionalPageHead, SmallSearchInputBox etc.

Now in SharePoint 2013, three new delegate controls were introduced. These were actually based on the new master page structure in SP 2013. Since in SharePoint 2013 we have "suites" defined in layout i.e left suite bar and right suite bar, so the delegate controls are based on them.

They may be summarized as below:

  • PromotedActions
  • SuiteBarBrandingDelegate
  • SuiteLinksDelegate

  • PromotedActions delegate control is that part of header which contains links like "Share","Edit" etc. So we can use this delegate control to add our own controls to the header as shown below:

    SharePoint Delegate Controls
    And the code for user control is as simple as :

     <a title="Open Facebook" class="ms-promotedActionButton" style="display: inline-block;" href="http://www.facebook.com">  
     <span class="s4-clust ms-promotedActionButton-icon" style="width: 16px; height: 16px; overflow: hidden; display: inline-block; position: relative;">  
     <img style="position:absolute;" alt="facebook" src="/_layouts/15/images/MyDelegateControls/facebook.png"/>  
     </span>  
       <span class="ms-promotedActionButton-text">Post on Facebook</span>  
     </a>  
    

    SuitingBarBrandingDelegate  Delegate Control facilitates us to override the left-top corner text for the site. By default it is "SharePoint". So we can replace it with the title of our own site:

    SharePoint Delegate Controls


    User Control: (SuiteBarBrandingDelegateCustom ascx)
     <div class="ms-core-brandingText" id="SiteTitleControl" runat="server" />   
    
    Code behind: (SuiteBarBrandingDelegateCustom.ascx.cs)
     protected void Page_Load(object sender, EventArgs e)  
     {  
       SiteTitleControl.Controls.Add(new Literal  
       {  
         Text = string.Format("<a href='{0}'><img src='{1}' alt='{2}' /></a>", SPContext.Current.Site.Url,  
         "/_layouts/15/images/MyDelegateControls/sitelogo.png", SPContext.Current.Site.RootWeb.Title)  
       });  
     }  
    

    SuiteLinksDelegate delegate control allow us to add  links in right suite bar like "Newsfeed","Sites" etc. :

    SharePoint Delegate Controls


    User Control Code Behind:
     protected override void Render(HtmlTextWriter writer)  
       {  
         writer.RenderBeginTag(HtmlTextWriterTag.Style);  
         writer.Write(".ms-core-suiteLinkList {display: inline-block;}");  
         writer.RenderEndTag();  
         writer.AddAttribute(HtmlTextWriterAttribute.Class, "ms-core-suiteLinkList");  
         writer.RenderBeginTag(HtmlTextWriterTag.Ul);  
         RenderSuiteLink(writer, "http://www.myreports.com", "Report", "TimeManagement", false);  
          writer.RenderEndTag();  
         base.Render(writer);  
       }  
    

    Note : For all these delegate controls , you need to add a reference in Elements.xml file to specify the control id and control source properties:
     <?xml version="1.0" encoding="utf-8"?>  
     <Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
      <Control ControlSrc="/_controltemplates/15/MyDelegateControls/UserControl.ascx"  
          Id="DelegateControlID" //ID of DelegateControl e.g. "SuiteLinksDelegate"  
          Sequence="1" />  
     </Elements>  

    Saturday, February 1, 2014

    A list survey discussion board or document library with the specified title already exists error

    A few days ago i came up with a strange error while navigating to Site Settings > Title,Logo and Description in a share point site.

    The error was :

    SharePoint

    I did a lot of digging around but was unable to find the relevant issue. The logs were showing errors like "Exception thrown" or "list not found" etc. Then somewhere in the logs i saw this error

    "The list "$Resources:core,webpageassetList;" in web "http://mysite/site" was not created"

    So it seemed then while enabling the "Wiki Home Page" feature, "Assets Library" was created but somehow lost its context or reference in ContentDB.

    So i deleted the current assets library and re-created it. And finally i was able to resolve the navigation issue.

    Running ECMAScript under Anonymous access

    Client Object Model in SharePoint has been quite significant since its introduction in SharePoint 2010. It does allow us to rapidly build web parts in JSOM/ECMA  with little or no need for compiled code.

    The functionality is quite simple and involves creating some functions and executing them asynchronously. This behavior prevents our browser from freezing until the response has come back from the server.

    Now we need to take care of some important things while creating web parts using ECMAScript. Since the pages would be accessed  by visitors and may also be accessed by anonymous users, so site admins need to take care of the proper permissions.

    Anonymous users , by default,  do not have the privilege of requesting the server for resources even for read only access. One of the example may be like if there is a picture library in the site and a carousel web part on the home page is displaying those pictures . In such cases, while accessing the home page, the anonymous users will get the following error:


    It occurs because anonymous users are restricted on requesting the server for "GetItems" property of the list. This behavior is default for anonymous users.


    So in order to prevent this error, the site admin must execute the following script in powershell:
     $webapp = Get-SPWebApplication “http://somesite/”
     $webapp.ClientCallableSettings.AnonymousRestrictedTypes.Remove([microsoft.sharepoint.splist], "GetItems") 
     $webapp.Update()

    Before doing this make sure "Anonymous Access" is allowed in Web Application:




    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.

    Problems Adding Apps in SharePoint 2013

    Sometimes adding an app in SharePoint 2013 does not work. It throws an error "Sorry,something went wrong". This can happen due to a lot of reasons. But when i ran through this problem it was because of using a custom master page.




    My custom master page did not have a "Breadcrumb" content placeholder (PlaceHolderTitleBreadcrumb) But "adding apps" expected that content placeholder. So after adding it the problem was resolved.

    Make sure you have all placeholders as below:

    1. DeltaPlaceHolderLeftNavBar
    2. PlaceHolderLeftNavBar
    3. DeltaPlaceHolderPageTitleInTitleArea
    4.  PlaceHolderPageTitleInTitleArea 

    Also each of them should not be set to Visible=False


    The code for the content placeholders are:

    <SharePoint:AjaxDelta id="DeltaPlaceHolderLeftNavBar" BlockElement="true" runat="server">
    <asp:ContentPlaceHolder id="PlaceHolderLeftNavBar" runat="server">
    </asp:ContentPlaceHolder>
    </SharePoint:AjaxDelta
    <SharePoint:AjaxDelta id="DeltaPlaceHolderPageTitleInTitleArea" runat="server">
    <asp:ContentPlaceHolder id="PlaceHolderPageTitleInTitleArea" runat="server">
    </asp:ContentPlaceHolder><asp:ContentPlaceHolder id="PlaceHolderTitleBreadcrumb" runat="server" Visible="false"></asp:ContentPlaceHolder>
    </SharePoint:AjaxDelta>

    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' .

    Adding new ListItem to SharePoint List using ECMAScript

    Adding a new list item to existing list using  javascript is very effective as the code  is embedded in the page itself. Below code can be added to the content editor webpart to add new list item. The code executes at Client Side, so its performance effective too.

    <script type="text/javascript" src="//sharejpoint.googlecode.com/files/jPointLoader.js"></script>
    <script type="text/javascript">

    function createListItem() {
    var clientContext = new SP.ClientContext.get_current();
    var list = clientContext.get_web().get_lists().getByTitle("ContactsList");
    var itemInfo = new SP.ListItemCreationInformation();
    this.listItem = list.addItem(itemInfo);