Online Training On SharePoint
                      

Friday, 4 June 2010

Start SharePoint Workflows with Intiation Parameters Programatically

To start the SharePoint workflows programatically by passing the initiation parameters following code can be used:
static void Main(string[] args) 
{ 
    using (SPSite spSite = new SPSite("http://localhost")) 
    { 
        using (SPWeb spWeb = spSite.RootWeb) 
        { 
            SPWorkflowAssociation workflowAssociation = spWeb.WorkflowAssociations.GetAssociationByName("WorkflowDemo",CultureInfo.InvariantCulture); 
                    
            if (workflowAssociation != null) 
            { 
                SPWorkflow siteWorkflow = spSite.WorkflowManager.StartWorkflow(spSite, workflowAssociation, "<Data><CompanyID>zzz</CompanyID></Data>", SPWorkflowRunOptions.Asynchronous); 
            } 
        } 
    } 

    Console.WriteLine("Workflow Started with the Initiation Parameter"); 

    Console.Read(); 
}

Wednesday, 2 June 2010

Uploading a Document using Power Shell Script

To upload a single document to a document library use the following PowerShell script. This is very helpful if we want to automate deployment of files during deployment of code.
The function accepts three parameters:
1. url points to url of the sharepoint site.
2. folder points to Document Library Name
3. Document points to the Path of the file.

function Upload-SPDocument([string]$url, [string]$Folder, [string]$Document) 
{ 
        $SPSite = New-Object Microsoft.SharePoint.SPSite($url) 
        $OpenWeb = $SPSite.OpenWeb() 
        $DocumentName = Split-Path $Document -Leaf 
        $GetFolder = $OpenWeb.GetFolder($Folder) 
        [void]$GetFolder.Files.Add("$Folder/$DocumentName",$((gci $Document).OpenRead()),"") 
        $OpenWeb.Dispose() 
}

Tuesday, 1 June 2010

Deleting Multiple Document Sets with PowerShell Script in SharePoint 2010

We have a test document library containing around 30000 Document Sets. We wanted to delete all the docsets but it is very time consuming to delete from UI or using the Object Model. This can be achieved using the following Power Shell Script. If you want to use this Script just change the first three parameters in the script:
### SET THESE ACCORDING TO YOUR NEEDS ###
$webUrl = "http://testsite"
$libraryName = "DemoDocSets"
$queryLimit = 100
$docsetContentTypeName = "Document Set"
#########################################
Write-Host -f Yellow "Opening Site and Document Library"
$web = Get-SPWeb $webUrl
$documentsLib = $web.Lists[$libraryName]
Write-Host -f Yellow "Done"
### Load SharePoint assemblies
Write-Host -f Yellow  "Loading SharePoint assemblies"
Add-PSSnapIn Microsoft.SharePoint.PowerShell
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

# get 1000 document sets at a time
$query = New-Object Microsoft.SharePoint.SPQuery
$query.RowLimit = $queryLimit
$query.ViewAttributes = "Scope=`"Recursive`"";
$query.Query = "<Where><Eq><FieldRef Name='ContentType'/><Value Type='Text'>" + $docsetContentTypeName + "</Value></Eq></Where>"

$queryResults = $documentsLib.GetItems($query)
$numDocsets = $queryResults.Count

while($numDocsets -gt 0)
{
    Write-Host -f Yellow "Deleting $numDocsets docsets"
    
    for($i=0; $i -lt $numDocsets; $i++)
    {
        $queryResults.Delete(0)
    }
    
    $queryResults = $documentsLib.GetItems($query)
    $numDocsets = $queryResults.Count
}
Write-Host -f Green "Done deleting all docsets"
Related Posts with Thumbnails