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

Monday, February 13, 2017

Enable / Setup Search Service in Sharepoint through Powershell

Sharepoint has provided an easy approach through Central admin to enable Search service application, but the one known issue with it is, it creates a database on its own. we can also enable this using powershell commands. By doing this, we can provide our own name for the DB.

Below is the step by step commands to be executed to enable Search service using powershell.
 #Add-PSSnapin Microsoft.SharePoint.PowerShell  
 # 1.Setting up some initial variables.  
 write-host 1.Setting up some initial variables.  
 $SSAName = "InternalSearch"  
 $SSADatabase = "InternalSearchDB"  
 $saAppPoolName = "SecurityTokenServiceApplicationPool"  
 $SSI = get-spenterprisesearchserviceinstance -local  
 $err = $null  
 # Start Services search services for SSI  
 write-host Start Services search services for SSI  
 Start-SPEnterpriseSearchServiceInstance -Identity $SSI  
 Start-SPEnterpriseSearchQueryAndSiteSettingsServiceInstance $SSI  
 # 2.connect to an Application Pool.  
 write-host 2.connect to an Application Pool.  
 $AppPool = Get-SPServiceApplicationPool $saAppPoolName  
 # 3.Create the SearchApplication and set it to a variable  
 write-host 3.Create the SearchApplication and set it to a variable  
 $SearchApp = New-SPEnterpriseSearchServiceApplication -Name $SSAName -applicationpool $AppPool -databasename $SSADatabase  
 #4 Create search service application proxy  
 write-host 4 Create search service application proxy  
 $SSAProxy = new-spenterprisesearchserviceapplicationproxy -name $SSAName" ApplicationProxy" -Uri $SearchApp.Uri.AbsoluteURI  
 # 5.Provision Search Admin Component.  
 write-host 5.Provision Search Admin Component.  
 set-SPenterprisesearchadministrationcomponent -searchapplication $SearchApp -searchserviceinstance $SSI  
 # 6.Create a new Crawl Topology.  
 write-host 6.Create a new Crawl Topology.  
 $CrawlTopo = $SearchApp | New-SPEnterpriseSearchCrawlTopology  
 # 7.Create a new Crawl Store.  
 write-host 7.Create a new Crawl Store.  
 $CrawlStore = $SearchApp | Get-SPEnterpriseSearchCrawlDatabase  
 # 8.Create a new Crawl Component.  
 write-host 8.Create a new Crawl Component.  
 New-SPEnterpriseSearchCrawlComponent -CrawlTopology $CrawlTopo -CrawlDatabase $CrawlStore -SearchServiceInstance $SSI  
 # 9.Activate the Crawl Topology.  
 write-host 9.Activate the Crawl Topology.  
 do  
 {  
   $err = $null  
   $CrawlTopo | Set-SPEnterpriseSearchCrawlTopology -Active -ErrorVariable err  
   if ($CrawlTopo.State -eq "Active")  
   {  
     $err = $null  
   }  
   Start-Sleep -Seconds 10  
 }  
 until ($err -eq $null)  
 # 10.Create a new Query Topology.  
 write-host 10.Create a new Query Topology.  
 $QueryTopo = $SearchApp | New-SPenterpriseSEarchQueryTopology -partitions 1  
 # 11.Create a variable for the Query Partition  
 write-host 11.Create a variable for the Query Partition  
 $Partition1 = ($QueryTopo | Get-SPEnterpriseSearchIndexPartition)  
 # 12.Create a Query Component.  
 write-host 12.Create a Query Component.  
 New-SPEnterpriseSearchQueryComponent -indexpartition $Partition1 -QueryTopology $QueryTopo -SearchServiceInstance $SSI  
 # 13.Create a variable for the Property Store DB.  
 write-host 13.Create a variable for the Property Store DB.  
 $PropDB = $SearchApp | Get-SPEnterpriseSearchPropertyDatabase  
 # 14.Set the Query Partition to use the Property Store DB.  
 write-host 14.Set the Query Partition to use the Property Store DB.  
 $Partition1 | Set-SPEnterpriseSearchIndexPartition -PropertyDatabase $PropDB  
 # 15.Activate the Query Topology.  
 write-host 15.Activate the Query Topology.  
 do  
 {  
   $err = $null  
   $QueryTopo | Set-SPEnterpriseSearchQueryTopology -Active -ErrorVariable err -ErrorAction SilentlyContinue  
   Start-Sleep -Seconds 10  
   if ($QueryTopo.State -eq "Active")  
     {  
       $err = $null  
     }  
 }  
 until ($err -eq $null)  
 Write-host "Your search application $SSAName is now ready"  

Sunday, February 12, 2017

Migrate Sharepoint User Account to a New Login Name

There might be many occasions where we have to change the Users' account, could be the domain change or name change.

 Also another scenario is, if we have enabled Farm based authentication sharepoint adds some special characters to the account in the start like 'i:0#.w|' and if  we disabled it at later point, these names will not be changed.  We need to migrate / change the account names manually before people start using it. If not, we see 2 accounts for individual users.

we can achieve this functionality using Poweshell script. Considering above scenario, below is the script used to migrate resources in bulk when we are moving back form Farm based to windows authentication.

Below script is to get all the people present in the current system to a CSV File.
 function GetSPWebUsers($SiteCollectionURL)   
 {   
   [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null   
   $site = new-object Microsoft.SharePoint.SPSite($SiteCollectionURL)   
   $web = $site.openweb()   
   $siteUsers = $web.SiteUsers   
 @(foreach ($user in $siteUsers) {  
   $usergroup = New-Object System.Object  
      $usergroup | Add-Member -type NoteProperty -name AccountName -value $user.LoginName  
    Write-Output $usergroup   
 }) | Export-Csv c:\userlist.csv -NoTypeInformation   
   $web.Dispose()   
   $site.Dispose()   
 }   
 GetSPWebUsers $args[0]  
 #to execute, run the command as below  
 #GetSPWebUsers "http://<<site collection URL>>"  

By running above script, we get all the users' login names to a CSV file which will be used as an input of below code where we migrate individual users to the windows accounts which are formed by removing the starting set of special characters "i:0#.w|"
 Add-PSSnapin Microsoft.SharePoint.PowerShell  
 function MigrateUserOrGroups($csvFile)  
 {  
   #Getting the SPFarm object  
   $farm = Get-SPFarm  
   Import-Csv $csvFile | ForEach-Object{  
   Write-Host "Migrating User" $_.login "to" $_.login.Substring(7) -ForegroundColor Green  
   $farm.MigrateUserAccount( $_.login, $_.login.Substring(7), $false )  
   Write-Host "Migration Completed" -ForegroundColor Cyan  
   }  
   # $farm.Name  
 }  
 MigrateUserOrGroups $args[0]  
 #to execute, run the command as below  
 #MigrateUserOrGroups "c:\userlist.csv"  

You can modify the code as per your requirement.

Thursday, February 9, 2017

Display Files / Folders from a Document Library in Sharepoint

Below is the code to display documents / folders from a library in a Sharepoint environment, in a Tree View.

we are using SPServices and JQuery to achieve this  functionality.

 <div id='status' style='color:#d41414;display:none;padding:5px'>  
 </div>  
 <div id='main-block' style='display:none'>  
      <div><h3>Documents</h3></div>  
      <div id="0">  
      </div>  
 </div>  
 <script src="/InternalResources/jquery.min.js"></script>  
 <script src="/InternalResources/jquery.SPServices-0.7.2.min.js"></script>  
 <script language="javascript" type="text/javascript">  
 $(document).ready(function(){  
      //Enter the list name. If url is different, please update url variable too.  
      //Here code takes the current context and current website  
   var list = "TestFolders";  
   var url = list + "/";   
      createTree(url, 0, list);       
  });  
 function createTree(url, tagID, list){  
   //get the url to define the level of tree,  
   $().SPServices({  
   operation: "GetListItems",  
   async: false,  
   listName: list,  
   CAMLViewFields: "<ViewFields><FieldRef Name='Title' /><FieldRef Name='ID' /><FieldRef Name='EncodedAbsUrl' /></ViewFields>",  
   CAMLQueryOptions: "<QueryOptions><Folder>" + url + "</Folder></QueryOptions>",  
   completefunc: function (xData, Status) {  
           if($(xData.responseXML).SPFilterNode("z:row").length > 0) {  
                $("#"+tagID+"").append("<ul id='prime-"+tagID+"'>");  
                $(xData.responseXML).SPFilterNode("z:row").each(function() {  
                     var id = $(this).attr("ows_ID");  
                     var title = $(this).attr("ows_Title");     
                     var folder =  $(this).attr("ows_FileLeafRef").split(";#")[1];   
                     $("#prime-"+tagID+"").append("<li id='"+id+"'>" + " <a href='"+$(this).attr("ows_EncodedAbsUrl")+"'>" + folder + " </a></li>");                  
                 var thisFSObjType = $(this).attr("ows_FSObjType").split(";#")[1];  
                 if(thisFSObjType == 1) {  
                     //auto call the function createTree again, to get subfolders and files, assigning a new url/subfolder and id  
                     createTree(url+'/'+folder, id, list);       
                 }  
                 });  
                 $("#"+tagID+"").append("</ul>");  
                 $('#status').css('display','none');  
                 $('#main-block').css('display','block');  
           }  
           else     {  
                $('#status').html('No documents found.');  
                $('#status').css('display','block');  
           }  
      }  
  });   
 }  
 </script>  

Sunday, January 22, 2017

Update List Items without modifing System columns in Sharepoint

Its most common we need to update few column values in backed without changing system columns like Modified By, Modified On in a Sharepoint list / document library.

In order to achieve this, we need to go with Item.SystemUpdate() instead of Item.Update() in your C# code.

In case, if its a one time activity and we need to update data in bulk, the easiest way is to go with PowerShell script. Below is the example to update a column value in a list without changing Modified On and Modified By values, using PowerShell.

 Add-PSSnapin microsoft.sharepoint.powershell  
 $web = Get-SPWeb "<<Site URL>>"  
 $list = $web.lists["<<List Name>>"]  
 #Get all items in particular list and save them to a variable.   
 #Here you can also apply your CAML query to fetch particular set of records  
 $items = $list.items  
 #Go through all list items  
 foreach($item in $items)  
 {       
      #If any conditions are required, can set them here in if clause.  
      #Update the fields as below.  
      $item["<<Field Display Name>>"] = "<<Value>>";  
      #This is the important change, using systemupdate will just set the above fields.  
      #This will not update Modified & Modified By Fields.  
      $item.SystemUpdate();  
 }  
 $web.Dispose();  

Thursday, August 13, 2015

Read & Update Display Names of List Columns through Javascript in Sharepoint

We can Read Columns (Internal Name, Display Name / Title, Type etc) & also Update Columns (Display Name / Title) of a List in Sharepoint ( 2010 & above ) using Client Object Model.

Below is the sample code in javascript to read Columns for a List.
 function retrieveAllFieldsOfList(listName) {  
   var ctx = SP.ClientContext.get_current();  
      var web = ctx.get_web();  
      var list = web.get_lists().getByTitle(listName);  
      var fields = list.get_fields();  
      ctx.load(fields, 'Include(Title,InternalName)');  
      ctx.executeQueryAsync(Function.createDelegate(this, this.Success), Function.createDelegate(this, this.Failure));  
 }  
 function Success()  
 {            
      var _fields = '';  
      var lEnum = fields.getEnumerator();  
      while(lEnum.moveNext())  
      {  
           var internalName = lEnum.get_current().get_internalName();   
           var title = lEnum.get_current().get_title();  
           _fields = _fields + 'Internal Name:' +internalName+ ',Title:' +title+ ';';  
      }  
      console.log(_fields);  
 }  
 function Failure(sender, args)  
 {  
      console.log("Failed" + args.get_message());  
 }  

Below is the Sample Code in javascript to Update Title / Display Name of a Field in a Sharepoint List
 function updateMonths(listName){  
      var ctx = SP.ClientContext.get_current();  
      var web = ctx.get_web();  
      var list = web.get_lists().getByTitle(listName);  
      //Below field section to be repeated for multiple field updates  
      var field = list.get_fields().getByInternalNameOrTitle('<Internal Name / Existing Title>');  
      field.set_title('<New Title>');  
      field.update();  
      ctx.executeQueryAsync(Function.createDelegate(this, this.Success), Function.createDelegate(this, this.Failure));  
 }  
 function Success()  
 {            
      console.log('Updated Successfully.');  
 }  
 function Failure(sender, args)  
 {  
      console.log("Failed" + args.get_message());  
 }  

Tuesday, August 11, 2015

Set Current User as default value in People Picker Sharepoint

We can't directly set default values for People picker fields in Sharepoint. We can achieve it using SPServices.

Below is the sample code to set logged in user name as default value for a People picker field

 <script src="../../Style Library/scripts/jquery.min.js" type="text/javascript"></script>  
 <script src="../../Style Library/scripts/jquery.SPServices-0.7.2.min.js" type="text/javascript"></script>  
 <script type="text/javascript">  
   $( document ).ready(function() {  
        //To set logged in user name as default value in a people picker field with display name, "Requested by"  
           //People Picker Field Display Name  
           var fieldName = 'Requested By';  
           //Check if the field is empty. As, this script gets executed each time the page loads(Could be during validations)  
           if(($().SPFindPeoplePicker({peoplePickerDisplayName: fieldName}).currentValue).trim() == '')  
           {  
                //Get current Logged in User  
                var currentUser = $().SPServices.SPGetCurrentUser({  
                                         fieldName: "Name",  
                                         debug: false  
                                    });  
                //Set logged in user name to the people picker   
                $().SPFindPeoplePicker({  
                      peoplePickerDisplayName: fieldName,  
                     valueToSet: currentUser,  
                     checkNames: true  
                });  
           }  
   });  
 </script>  

Access this link to know more about the SPServices method 'SPFindPeoplePicker' used to set people picker

Don't forget to check if the field value is empty before you set, as the end user might have changed the name and when the user clicks on Submit, if there are any validation errors, page reloads and the script gets executed.

Friday, June 26, 2015

Link Data in PowerPoint to the Source (Database / Sharepoint)

It’s very common to have PowerPoint Presentations for High level Meetings.  We even have lots of data represented in these presentations in the form of Charts / Tables.  And, this would be a recurring process to update the presentations with updated data. This takes lots of manual efforts.

We can automate process of updating the presentations with latest data to a fair extent with available out of the box features. It requires minimal development efforts. 

On a high level, we first create Excel sheet with all the required data being fetched from different sources like Databases / Sharepoint using Data Connections. This would provide auto updating of data to Excels. Also, using the data we create different views, could be charts / Pivot tables or any other as per our representation need. After our excel sheet with different charts / tables is ready, we insert these into our Presentation by maintaining the links to the Excel we created. Linking would provide auto updating of data from excel to our Objects on PowerPoint, which is actually connected to main data source (database / Sharepoint list). 

With these efforts, we are done and now our Presentation is ready and also the data in it is up to date.  Isn’t it simple to implement and saves lots of our efforts, we spend in regular intervals to update the data on it?

Now let’s try to implement the same with a simple example to understand it better. 
Here our goal is to display Sharepoint data in our Monthly Dashboards which are actually PowerPoint Presentations.

Step 1: Prepare your data connections ready to bind to actual real time data (Sharepoint List here)
  • Open Sharepoint List which has our required data.
  • Create a view with required fields and also if there are any filters to be applied, you can do that here.
  • Now open the view we just created and in the ribbon, under ‘List’ tab, we have a button to ‘Export to Excel’.
  • Click on it. A web Query file (.iqy) is downloaded. Please save into your local machine. Let’s rename this file to ‘DashboardData.iqy’

Step 2: Create excel sheet with required representations (Charts / tables) using above created Connections
  • Open excel file and in ribbon, under data tab, Click on ‘Connections button’
  • A window with available connections to the workbook is opened. Click on ‘Add..’>
  • Another child window opens with all available connection files in our local machine.
  • Click on ‘Browse for More’ button, a file browser window opens, now select the ‘DashboardData.iqy’ file we downloaded in step 1.
  • You can see the newly added connection listed in Workbook Connections window
  • With this connection you can create pivot table or Pivot charts. For example, under ‘Insert’ tab in Excel, click on ‘PivotChart’ available in ‘Tables’ section. A window to select data source is opened. Now select the above created connection.
  • Now a pivot table with data from Sharepoint list is available with all fields we added in the List view of Sharepoint
  • Create chart with required fields and save the Excel file
  • In similar way we can create multiple charts as per our need. We can also bind data to the sheet and using Conditional Formatting, we can represent cell values as per requirement.
  • After all the required charts / tables created , save the file

Step 3: Insert the tables / charts we created in excel to the PowerPoint presentations with links
  • Open existing PowerPoint presentation / new one and add all the required sections / content into it.
  • In the sections where we need to add the dynamic content i.e. charts / tables, we bring it form excel file.
  • As an example, copy the Pivot chart we created in excel and go to the powerpoint presentation we are working on
  • In ribbon, under ‘Home’ tab, we have ‘Paste Special’ button in ‘Clipboard’ section. Click on it
  • A window with option to paste as object and link feature is opened
  • Select ‘Paste link’ and click on Ok.
  • Now move the chart object to the required section and also adjust the size etc.
  • Similarly repeat the same step for all required objects
  • Save the Presentation File.
With these efforts, our presentation is ready with data linked to Actual data sources.



If you need the presentation with updated data, simply open the excel we created in step 2 and refresh all the data connections in it. All objects in the presentation will be automatically updated with latest data.

To refresh the connections, open excel file and under ‘data’ tab, we have ‘Refresh all’ button. Click on it. We can also automate this using some macros.

Now it’s your turn to automate the Presentations you update regularly!! 

Thursday, June 11, 2015

Sharepoint, Hide List or library in Site Content Pages

While developing applications on Sharepoint, there might be a requirement to create some lists for Internal use and they have to be hidden from end users in Web front end i.e. in View All Site content screens etc.

To achieve this functionality, we need Sharepoint designer. Before implementing it, we need to hide the list from left navigation. To do this, go to 'List settings' and  'Title description and navigation' and select 'No' for 'Display this list on the Quick Launch?', then click Save.  

  • Now open the site in Sharepoint Designer 
  • In left navigation click on 'Lists and Libraries'
  • All lists in the existing site are displayed along with Document libraries
  • Now click on the Library / List which you want to hide
  • A screen with the corresponding List settings is opened
  • In Settings section , available to left , Under 'General Settings' category, an option 'Hide from Browser' is available. 
  • Select it and save the page.

With above changes the list is hidden in All site content Page and other site pages.
By doing this, we are just not listing it along with other Lists, still you can access the list if you have its corresponding web link.

You also need to note that due to this change, it will also be hidden in Designer under 'Lists and Libraries' navigation block.
To access it in designer / to list it back, follow below steps.
  • Open Site in Designer
  • Click on 'All Files' in left navigation.
  • Now click on 'Lists'
  • Here you can see the hidden Lists too.
  • If its library, it will be listed when we click on 'All files'
  •  Now right click on the hidden list and go to 'Properties'
  • Now the screen with List details and Settings is opened, same as earlier
  • Here uncheck the option 'Hide from Browser' and save
  • Now it would be available in All content and also in designer

Wednesday, May 13, 2015

Sharepoint Workflow running on Old Version after publishing

Observed a weird behavior in Sharepoint Designer workflows. We have Workflows designed on designer and being used in our application. There are multiple versions on it as we added new features time to time. Recently we added some more features and published to latest version. It updated to server and could see latest version in Workflows section of the corresponding List in website.

But, when we run the workflow, we observed that its of too old version, many latest features were missing.

After doing some online research, we realized that the problem is due to cache of workflow activities in the local machine where we implemented them using Designer.

Sometimes, SPD might be using the old DLL fro mthe cache than the updated one and thus we see old version running. To correct this issue, we need to clear the Cache and run the Designer and republish the workflow. And also, its better to disable option to cache site data in designer, to avoid similar problems in future. To do this, follow below steps.

  • If Sharepoint Designer is open, close it
  • Open My Computer and enter '%USERPROFILE%\AppData\Local\Microsoft\WebsiteCache' in address bar
  • Delete files and other folders in this location
  • Now go to this location '%APPDATA%\Microsoft\Web Server Extensions\Cache' and delete all fiels and folders
This would solve the issue and now if you re publish the workflow, it would work fine.

To disable Site Data caching, follow below steps

  • Navigate to the "File" menu then select "Options" -> "General" -> "Application Options"
  • On the “General” tab, under the “General” heading, uncheck “Cache site data across SharePoint Designer sessions” 

Friday, February 20, 2015

Create a List from Custom Template using Powershell in Sharepoint

Lets consider a situation where we have lots of team sites under a Site collection. Now a new requirement has come and we want to store Team Updates in their corresponding sites.

To do this, creating the custom list with required fields in every list is a long time process. If we create a list in one site and save it as template, we can create New list using this template in other sites. Still manually opening all sites and creating a new list will take long time.

This creation part can be achieved using Powershell. For this, lets assume we created a list and saved it as template. This template will be available in site collection. We can also import List template to Site Collection gallery from external.

Now, open the server and use below Powershell commands in Sharepoint Management Studio

 //Get Site collection through SPSite command  
 $spSite = get-spsite("http://Site Collection URL")  

 //Get current website into which new template to be added  
 $SPWeb = Get-SPWeb("http://Site URL") 
 
 //Get list of all custom templates existing in the site collection  
 $listTemplates = $spsite.GetCustomListTemplates($spweb)  

 //Create list using the above templates, by selecting the custom template you are looking for  
 $SPWeb.Lists.Add("Title of List","Description",$listTemplates["Template Name"])
  

Above example is shown for a single site. We can loop the command for list of sites by storing them into an array or if for all subsites, we can fetch them through command and loop.

As our List template is custom one, we need to follow above steps. If we want to create list from sharepoint's default templates, we can use below commands

 //Get current website into which new template to be added  
 $SPWeb = Get-SPSite("http://SiteURL")  

 //Get list of all templates. Its just for your reference to see list  
 $SPWeb.ListTemplates | Select Name, type, type_client, Description  

 //Create list using the template name. The template name should be in above list  
 $SPWeb.Lists.Add("Title","Description","Template Name")  


You can also save an existing list as a template using below commands

 //Get current website in which the list is created  
 $SPWeb = Get-SPSite("http://SiteURL")  

 //Get the list by using list name which to be saved as template  
 $list = $web.Lists["List Name"]  

 //Use below command to save it as template.   
 //Lat parameter ( 1 / 0 ) indicates if to be stored with data or without data  
 $list.SaveAsTemplate(“Template Name”,”Template Title”,”Template Description”,1)  


Wednesday, February 18, 2015

Calculated Column & Caclulated value in Sharepoint 2010

We have Calculated columns in Sharepoint. The data in these columns can be set based on other column Information. These columns will not be displayed in New / Edit forms as the data in these columns and are calculated internally according to the formula provided by us, before saving the record in database.

You can see simple example in below image, where we are fetching Request Created Day based on created date field.


Use this link to know more about the formulas for calculated columns.

One important point to remember about these columns is, The calculation of the value is done only when an Item created and updated. So, we can't use general functions like 'Today' , 'Me' in the formula.

We can also set Calculated value for Default value to a column. If there are any requirements where default value is to be calculated, we use this.

As an example, if we want to set default value for a title in Status list with Date in it as "Status as on [Date]", we can use below formula in Default value by selecting 'Calculated Value'

 ="Status as on "&TEXT(Today,"dd/mm/yyyy")


Important point to remember here is, We can't use other columns in these formulas as these are calculated before the Edit / New screen loaded and by then we don't have data in other columns.

We can use generic formula like 'Today' or 'Me' and the value set with corresponding data when the screen loaded.

Wednesday, February 11, 2015

Custom Timer job in Sharepoint 2010 using Visual Studio

Sharepoint provided a platform to develop timer jobs and deploy in its environment. We can monitor the jobs in central admin along with other jobs. Scheduler configuration is also possible through central admin.

To manage timer jobs, acces to Central Administrator > Monitoring > Review Job definitions
And to monitor job executions, Central Administrator > Monitoring > Check Job Status


Let’s create a sample Timer job and deploy it for better understanding.

Requirement: We have project sites for each project where their health statuses etc. are maintained in predefined template lists.  Now, customers need a dashboard to look into the project status at one place along with individual sites’ links.

Approach: For this, we are creating a list with required details in parent site and we develop a Timer Job to which runs at certain interval and updates the statuses of all sites into this list and a dashboard is created using this list.

Step 1: Create ‘Project Statistics’ list in Site collection / Parent site and ‘Project Details’ in child site (Individual project sites)

Step 2: Now we need to create a Timer job. To do this, follow below steps
  1. Open Visual Studio and create a new project using  ‘Empty Sharepoint Project’ Template

  2. If Sharepoint has single Server, provide the web application where it has to be deployed and don’t forget to select ‘Deploy as a Farm Solution’ and then click on ‘Finish'

    Note: If site url is not provided, we can package it and deploy the WSP solution using Powershell.
  3. Now, create a new class file in visual studio and extend the class from ‘SPJobDefinition’. Use below code as sample reference
         public StatusUpdateTimerJob()  
           : base()  
         {  
         }  
         public StatusUpdateTimerJob(string jobName, SPService service,  
             SPServer server, SPJobLockType lockType)  
             : base(jobName, service, server, lockType)  
         {  
           this.Title = "Project Status Update Timer Job";  
         }  
         public StatusUpdateTimerJob(string jobName, SPWebApplication webapp)  
           : base(jobName, webapp, null, SPJobLockType.ContentDatabase)  
         {  
           this.Title = "Project Status Update Timer Job";  
         }  
         public override void Execute(Guid targetInstanceId)  
         {  
           //Write your logic to delete existing records from main list (Project Statistics) in parent site and then  
           //Iterate through all child sites and insert new records to above list from 'Project Details' list  
         }  
    
    Above code has 3 overloaded constructors which will be called internally as per need and write your logic in Execute method
  4. Now we create a feature to install and uninstall the Job. To do this, right click on ‘Features’ in solution explorer or Visual studio and click ‘Add Feature’

    Provide Feature Title and Description , this will be displayed in Features list of a site collection and select scope to ‘Site’


    In the event Receiver class of the above created feature, replace the code with below
     const string JobName = "ProjectStatus Update Timer Job";  
     public override void FeatureActivated(SPFeatureReceiverProperties properties)  
     {  
          SPSite site = properties.Feature.Parent as SPSite;  
          DeleteJob(site); // Delete Job if already Exists  
          CreateJob(site); // Create new Job  
     }  
     private static void DeleteJob(SPSite site)  
     {  
          foreach (SPJobDefinition job in site.WebApplication.JobDefinitions)  
               if (job.Name == JobName)  
                    job.Delete();  
     }  
     private static void CreateJob(SPSite site)  
     {  
          StatusUpdateTimerJob job = new StatusUpdateTimerJob(JobName, site.WebApplication);  
          SPMinuteSchedule schedule = new SPMinuteSchedule();  
          schedule.BeginSecond = 0;  
          schedule.EndSecond = 5;  
          schedule.Interval = 5;  
          job.Schedule = schedule;  
          job.Update();  
     }  
     public override void FeatureDeactivating(SPFeatureReceiverProperties properties)  
     {  
          DeleteJob(properties.Feature.Parent as SPSite); // Delete the Job  
     }  
    

    The above methods are to Create Job and Delete job which are called when feature activated and deactivated accordingly.
  5. Now are ready with our Timer job and we have to deploy it in the server. We can deploy it directly from Visual studio using ‘deploy’ Option in ‘debug ‘ or by packaging and installing the Solution
    To install Solution, use below Powershell commands
     Add-SPSolution -LiteralPath c:\SampleTImerjob.wsp  
     Install-SPSolution -Identity SampleTImerjob.wsp -GACDeployment  
     enable-spfeature -identity SampleTImerjob _Feature1 -url ‘Site collection URL’  
    
    We deployed it in GAC, as it’s a Timer job and should be accessed globally.

    Note: 1st 2 commands should be executed in one window and the last one should be in a new window as the dll is deployed in GAC, it will not reflect for the powershell window if we try to enable in same window
Step 3: Using above commands, we even activated the feature. So, the job will be created and it is scheduled. You can see it in ‘Central Admin’ as explained in the starting

We can also reconfigure the schedule by clicking on the title.


With this we implemented our requirement and for every 2 minutes, statuses for all project sites are updated in parent site and the dashboard is prepared on top of the list.

One important point to not to forget is, if we are modifying the Timer job, we have to update the Assembly version number. If not, the changes might not reflect properly

Thursday, January 8, 2015

Word Document Creation Dynamically in Sharepoint 2010

Though I have been working on SharePoint from past 4+ years, I didn't notice this awesome feature, We can create / update word documents dynamically in SharePoint. This is a very useful feature which will be helpful in creation of standard word documents like Approval forms / Employee Offer letters etc. It just takes few minutes to setup.

Lets try to create one sample form (Employee Offer Letter) using this feature.

I'm implementing this example in Sharepoint 2010. We need Design rights on SharePoint site and also Sharepoint Designer, MS Word  are needed for this example.

 Step 1: Create a Document library to generate and store the Offer letter documents in the  site. Create the columns that are dynamically need to be updated in the document, eg: Name, salary etc. The columns in Library acts as properties for a document, which can be added directly in the word document. In this example I added 4 such fields
      1. Employee Name
      2. Salary
      3. Designation
      4. Proposed Joining Date



 Step 2: For every Document library we will have a default file template for that library. This can be word document too. To edit it, go to Settings > Advanced Settings . Document template will be available, click on 'Edit Template'. This opens up MS Word with the standard template file (.dotx)


 Step 3: Now, writeup the standard content that to be there in the Offer letter and in the places where data to be filled dynamically i.e. Employee name, salary etc. insert the Document Properties that we created in above steps. To add a document property, Select 'Insert' in word ribbon and Go to Quick Parts > Document Properties. All the above created columns are displayed along with default ones. Select corresponding property.


Step 4: Once added , click on Save as and change the format from '.dotx' to '.docx' ( this step is needed to automate document generation, we can't update a '.dotx' from back-end.) Now the document looks as in below screen.



With above step, we are ready with document library and if we create a new document in this library, it will automatically fill in all the corresponding placeholders and our document is ready.

Step 5: Now, create a document to test it. If you click on 'New Document' in the document library, a word document with previously created template opens. Also, a form with all the new fields we created will be displayed on top.




Step 6: If we fill-in the corresponding details,  the place holders will be automatically updated and once you save it, a new document with these details is created in the document library. A sample document will appear as in below screen after filling all details.




To, create this document dynamically, we need to create new item through back end in the library, by filling all the properties we created. This can be achieved in multiple ways. 

I created a list with same details and using a workflow on the list, when a new list item created,  it will create a new item in the document library by filling all corresponding properties in library.

To do this, follow below steps. 

Step 7: Create a Sharepoint list with the same columns we created for library.



Step 8: Open Sharepoint designer and Go to the list we created and click on New workflow. Fill in the Workflow Name, description

Step 9: Select action 'Create List Item' from Actions tab in ribbon

Step 10: Click on 'this list' and choose the document library we created.

Step 11: Using 'Add' option, choose each column and fill it with the current item corresponding column data. Also, modify default field 'Path and Name' with the convention you prefer.

Step 12: Once all the fields are mapped. Save the workflow. And, select the option to 'Start workflow automatically when item created' and publish it. I also added another step to send email with the newly created document link.

With these steps we completed our configuration / development process for the functionality.

To test it, create a new item in the list with  all the details. Then, open our document library to see the newly created document with the details.

This feature not only automates document creation, but also helps us in logging the details in online system which will be helpful for future references / audit purposes.