Saturday, September 20, 2014

Client Object Model sample codes

Hi there,

I worked for few items in the client object model using ECMA script and here are the sample codes I am sharing for the example.

The below code gives you the web title and web id.


The below code adds the new list to the site with updating the list title, description, template and then added to the web,




SharePoint Master Page Dynamic footer using Delegate controls

Hi there,

I had worked on the requirement to display the custom information like site owner, contact number, email and last date modified in the master page footer information.

So all the data related to the site owner, contact number, email are dynamic and when the new person takes the site owner role, our code should be smart enough to update this information rather than changing the master page file.

Here we used the delegate controls to get the information from the custom list where the items contains the site owner name, contact, email so that the delegate control is able to render this information in the master page footer control.

For adding the delegate controls in the master page, first we have to create the user control and then reference it in the master page with attribute ControlID.


Create the user control with mapping the ControlTemplates folder in the visual studio and then deploy this control with specifying the path using module and elements.xml.

Here is the elements.xml from the module file.

See the attributes Id mapped to the delegate control in the master page.

Now the FooterControl.ascx file has its own custom logic to fetch the information from the  custom list based on the current web context and able to render the same on the master page.

Here is the sample code used for Footer user control page load method. Here the custom list is located in the root web and it is named as SiteOwner with fields Web title, owner name, email, contact.

Final output:





Custom Timer Job Adding new items for the custom list on the schedule time

Hi there,

I worked on the small requirement to add the items for a custom list with predefined schedule.

Here we used the base class SPJobDefinition which is part of SharePoint, Administration namespace.
So we need to make sure to add the reference Microsoft.SharePoint.Administration reference and the namespace as well.

our custom timer job class needs to inherit the base class SPJobDefinition and then it has to override the method Execute().

Here is the sample code we have to write for our business logic and then associate this class with feature receiver.



Regarding the feature scope, since this job runs under the central administration, we need to activate this feature at the web application level for which our custom list is getting updated





Add publishing pages and web parts to the site programatically

Hi Guys

I had got the chance to work for adding the publishing pages to the given site through feature activation so that when the custom solution got deployed, the share point site is automatically associated with the custom web parts along with these publishing pages.

This is the sample code I wrote for my development purpose.
Please make sure that the publishing feature is activated on the site collection level and the site level.
In the code snippet, we need to use PublishingWeb class, which is component of Microsoft.SharePoint.Publishing.dll.
Make sure to add that dll and that namespace as well in your code to work as expected.




Here is the next code to add the specific web part to the existing page programatically.
Here we used the SPLimitedWebPartManager class to add the web part to the existing page.

Sunday, June 22, 2014

Disable Throttling Limit for specific lists

Hi All,

Recently, we had the requirement to disable the large query window for entire web application as it is having some performance issues over content database.

But, we have some large lists used by business, which is stopping us to enable the throttling setting at the web application level.

We have the propery at the list level to disable throttling limit for disabling throttling limit so that these lists will not have any impact though we disable the large window time at web application level.

The propery name is "EnableThrottling" which we need to set it to false.

Here is the simple powershell script with the example.

 
$web=get-spweb http://sitecollectionurl
$list=$web.Lists[“ListName”];
$list.EnableThrottling=$false;
$list.update();

Workflow Instance Report across site collection using Powershell script

Hi All,

I have written the script to retrieve the workflow instances that associated across a specific site collection.

The script will iterate through each item in the list/library for all sub sites in the site collection and provides the workflows associated with the status, status url, item id, workflow name, workflow instance id.

 
param ([boolean] $writeToFile = $true)
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Add-Type @'
 public class workflowItem
 {
   public string weburl;
   public string ListURL;
   public string Item;
   public string workflowname;
   public string status;
   public string InstanceId;
   public string url;
 }
'@

$itemsCollection = New-Object 'System.Collections.Generic.List[System.Object]';
$exportfile = $currentdatetime = get-date -format "yyyy-MM-d.hh-mm-ss";
$exportVersionLocation=$exportfolder+"\workflowstats"+$exportfile+".csv"

#Counter variables
 $webcount = 0
 $listcount = 0
 $associationcount = 0

$sitename =  Read-Host "Enter site collection url"
$site=Get-SPSite -Identity $sitename

$webs=$site.Allwebs

if($webs.count -ge 1)
{

foreach($web in $site.Allwebs)
{


 write-host $web 
 #Grab all lists in the current web
 $lists = $web.Lists
 foreach($list in $lists)
 {
 $associations = @()
    #Get all workflows that are associated with the current list
             foreach($listassociation in $list.WorkflowAssociations)
 {
 $associations += $($listassociation.name)
 }
 $listcount +=1

 if($associations.count -ge 1 -and $list.items.count -ge 1)
 {

 foreach($item in $list.items)
  {

foreach($wf in $item.workflows)
    {

     $wfItem = New-Object workflowItem
     $wfItem.weburl=$web.url
     $wfItem.ListURL=$list.Title
     $wfitem.Item=$item.Title
     $ass1=$wf.parentassociation
     $wfItem.workflowname=$ass1.Name
     $wfItem.status=$wf.InternalState
     $wfitem.InstanceId=$wf.InstanceID.Tostring()
     $wfitem.url=$wf.StatusUrl
     $itemsCollection.Add($wfItem);
     
    }
 
   }

 
 }

 }
}
 $webcount +=1

 $web.Dispose()
 }

$itemsCollection | export-csv "$exportVersionLocation" -noType
 
The csv file will be generated in the root directory where the script placed and executed,

Large List items paging using ListItemCollectionPosition

Hi All,

I got the requirement to iterate large list items which are above throttling limits. Since we get the throttling exception when try to retrive items using caml query through standard process using server object model.

I used the "ListItemCollectionPosition" object to show items in the paging view.


 
$weburl=Read-Host "Enter site url"
$web = Get-SPWeb -Identity $weburl
$listname=Read-Host "Enter the list name"
$list = $web.Lists["$listname"]
if($list -ne $null)

{
write-host $list.Title
$spQuery = New-Object Microsoft.SharePoint.SPQuery
$spQuery.ViewAttributes = "Scope='Recursive'";
$spQuery.RowLimit = 10
$caml = '' 
$spQuery.Query = $caml 

do
{
    
    $listItems = $list.GetItems($spQuery)
    
    Write-Host $listItems.ListItemCollectionPosition.PagingInfo
    $spQuery.ListItemCollectionPosition = $listItems.ListItemCollectionPosition
    foreach($item in $listItems)
    {
        Write-Host $item.Title
        
    }
}
while ($spQuery.ListItemCollectionPosition -ne $null)
}

else
{
write-host "List does not exist"
}