Tuesday, October 22, 2013

Create a SharePoint Custom Action to Zip and Download a Document Set

Here’s the URL to the built-in document set downloader page: {SiteUrl}/_layouts/DocSetExport.aspx?List={ListId}&ID={ItemId}. It takes in the GUID of the list and the ID of the document set. Here’s an example: http://sharepoint2010/sites/docman/_layouts/DocSetExport.aspx?List={4E2D11F5-7500-476A-B001-62A7BF92FD50}&ID=10
Adding a Custom Action to the Document Set List Item Menu using SharePoint Designer
To add a custom action, open the site in SharePoint Designer and navigate to the document set list. Add a new list item menu custom action:
image
Provide a Name, Description, and Navigate to URL:
image
Save the custom action, and test it:
image
image
I want to mention that there is a drawback to this approach. The custom action will show up for every list item, not just the document set. The list item menu will appear on documents in the document set as well:
image
Adding a Custom Action to the Document Set Ribbon using SharePoint Designer
We can also use SharePoint Designer to add a button to the document set ribbon, but the user experience isn’t ideal. You’re better off creating this custom action in a sandboxed (or farm) solution. The reason for this is that custom actions created in SharePoint Designer operate on a list item, so the user has to select a list item before the ribbon button lights up to zip and download the document set. Understanding the limitations, here’s how to do it:
Add a new view ribbon custom action:
image
Enter a Name and Description as before, but this time use JavaScript for the custom action. We need script to get the ID of the document set rather than the ID of the selected item. Enter the following for the Navigate to URL:
javascript:function getQS(key){var regex=new RegExp('[\\?&]'+key+'=([^&#]*)');var qs=regex.exec(window.location.href);return qs[1];}var url='{SiteUrl}/_layouts/DocSetExport.aspx?List={ListId}&ID='+getQS("ID");window.location.href=url;
Now when you select a document the zip and download button lights up:
image
Adding Custom Actions to the Document Set List Item Menu using a Sandboxed Solution
Next, I’ll show you how to create a sandboxed solution that provides a better user experience and is reusable across sites. The solution deploys both the list item and document set ribbon and scopes the custom action to just the document set content type. To get started, create a new empty SharePoint project in Visual Studio 2010. Add a feature and name it DocSetDownload. Add a new Elements item named CustomActions. Add the following to the Elements.xml file
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
    Id="DocSetDownload.Ribbon"
    Title="Zip and Download"
    Description="Zip all documents in this document set and download."
    RegistrationType="ContentType"
    RegistrationId="0x0120D520"
    Location="CommandUI.Ribbon">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition
          Location="Ribbon.ManageDocumentSet.MDS.Share.Controls._children">
          <Button
            Id="Ribbon.ManageDocumentSet.MDS.Share.Controls.DocSetDownload"
            Sequence="20"
            Command="Ribbon.ManageDocumentSet.MDS.Share.DocSetDownload"
            Alt="Zip and Download"
            Image16by16="images/DocSetDownload16x16.png"
            Image32by32="images/DocSetDownload32x32.png"
            LabelText="Zip and Download"
            ToolTipTitle="Zip and Download"
            ToolTipDescription="Zip all documents in this document set and download."
            TemplateAlias="o1" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler
          Command="Ribbon.ManageDocumentSet.MDS.Share.DocSetDownload"
          EnabledScript="true"
          CommandAction="javascript:
          function getQS(key){
            var regex=new RegExp('[\\?&amp;]'+key+'=([^&amp;#]*)');
            var qs=regex.exec(window.location.href);
            return qs[1];
          }
          var url='{SiteUrl}/_layouts/DocSetExport.aspx?List={ListId}&amp;ID='+getQS('ID');
          window.location.href=url;
          " />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
  <CustomAction
    Id="DocSetDownload.EditControlBlock"
    Title="Zip and Download"
    Description="Zip all documents in this document set and download."
    RegistrationType="ContentType"
    RegistrationId="0x0120D520"
    Location="EditControlBlock">
    <UrlAction Url="{SiteUrl}/_layouts/DocSetExport.aspx?List={ListId}&amp;ID={ItemId}" />
  </CustomAction>
</Elements>
Next, add a Module item named images to the project and add the custom images for the ribbon:
image
Deploy and test the solution. You’ll see the custom action on the ribbon:
image
The custom action will also appear on the list item menu:
image

http://blogs.msdn.com/b/johnwpowell/archive/2012/01/21/create-a-sharepoint-custom-action-to-zip-and-download-a-document-set.aspx

Monday, October 21, 2013

Update Hyperlink Field in SharePoint with PowerShell

I want to change the URL in list item from https://Texas.com to https://ta.com using Powershell script

1) copy & paste (change the URLs and Listnames and field) the below script and name it with extension .ps1 (ex: ItemURLChange.ps1)


#Add-PSSnapin Microsoft.SharePoint.PowerShell

$siteUrl = "http://sharepoint/SiteCollection/SiteName"
$webName = “SiteName”
$listName = "Name of your list"
$spSite = new-object Microsoft.SharePoint.SPSite($siteurl)
$spWeb = $spSite.OpenWeb($webName)
$spList = $spWeb.Lists[$listName]

foreach($Item in $spList.Items )
{
$ofldurl= new-object Microsoft.SharePoint.SPFieldUrlValue($Item["URL"])

$ofldurl.URL = $ofldurl.URL.Replace("Texas", "ta")
$ofldurl.Description = $ofldurl.Description.Replace("Texas", "ta")

$item["URL"] = $ofldurl
$item.update()
}

$spWeb.Dispose()

2) Open the Powershell command and run using the below command

PS C:\temp> ./ItemURLChange


http://www.sharepointed.com/2011/09/09/update-hyperlink-field-in-sharepoint-with-powershell/