Tuesday, 29 July 2014

Deploy Sharepoint Solution using powershell script. This powershell script will deploy the solution globally..

## check to ensure Microsoft.SharePoint.PowerShell is loaded if not using the
SharePoint Management Shell
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq
'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null)
{   
 Write-Host "Loading SharePoint Powershell Snapin"   
 Add-PSSnapin "Microsoft.SharePoint.Powershell"
}


## Read the Farm parameters
$SolutionName = "SolutionName1.wsp"
## Store current directory
$CurrentDirectory= get-location
$SolutionsFolder = $CurrentDirectory
$Scope="Global"
## Create the log file at the desired location with current date and time format.
$LogFileName =  "C:\Logs\DeploySolutionName-$(get-date -format MMddyyHHmmss).log"
Set-Content -Path $LogFileName "DeploySolution Script Execution starts at $(get-
date -format MM-dd-yyyy-HH:mm:ss) ...."
# Log generation.
function Write-Log
{
param ([string]$info, [bool]$logReq)
Add-Content -Path $LogFileName -value $info  
   if($logReq )
   {
     Write-Host $info
   }
}

## this method will delete and retract the solution.
function DeleteSolution
{
 param ([string]$solutionName)
    $solution = (Get-SPFarm).Solutions[$solutionName]
   
    #Check if solution is present in the solution store or not.
 if($solution)
 { 
        Write-Log -info "Initiating to uninstall $solutionName..." -logReq $true
  if( $solution.Deployed -eq $true )
  {
   Uninstall-SPSolution -Identity $solutionName -Confirm:1
  
   while( $solution.JobExists )
   {   
                Write-Log -info "Waiting for retraction of $solutionName..." -
logReq $true
    sleep 5
   }
  }
 
        Write-Log -info "Initiating the removal of $solutionName..." -logReq $true
  Remove-SPSolution -Identity $solutionName -Force -Confirm:1
 
        Write-Log -info "$solutionName is deleted from this farm." -logReq $true
 }
 else
 { 
        Write-Log -info "$solutionName not found in the solution store." -logReq
$true
 }
}
## This method installs and deploys a solution based on its solution id and path.
function DeploySolution
{
 param ([string]$solutionName, [string]$solutionPath)
 $filename = $solutionPath + "\" + $solutionName

    Write-Log -info "Initiating to add solution $solutionName from $filename
globally..." -logReq $true
 Add-SPSolution -LiteralPath $filename

    Write-Log -info "$solutionName added successfully to the solution store." -
logReq $true

    Write-Log -info "Initiating the installation of $solutionName globally." -
logReq $true

 Install-SPSolution –Identity $solutionName -GACDeployment -Force
   
    $solution = (Get-SPFarm).Solutions[$solutionName]

 if((Get-SPFarm).Solutions[$solutionName])
 { 
        while( $solution.Deployed -eq $false )
  {
   try
   {   
                Write-Log -info "Waiting for installation of $solutionName..." -
logReq $true
    sleep 5
   }
   catch
            {             
              Write-Log -info "Installation of $solutionName has failed. Please see
central admin for more details." -logReq $true
              Write-Log -info $_ -logReq $true
            }
  }
 
        Write-Log -info "$solutionName is installed successfully in the farm." -
logReq $true
 }
 else
 { 
        Write-Log -info "$solutionName not found in the solution store." -logReq
$true
 }
}
##Start Administration Service.
$AdminServiceName = "SPAdminV4"
$IsAdminServiceWasRunning = $true;

if ($(Get-Service $AdminServiceName).Status -eq "Stopped")
{
     $IsAdminServiceWasRunning = $false;
     Start-Service $AdminServiceName  
     Write-Log -info "SharePoint 2010 Administration service was stopped, so
started now." -logReq $true
}
##Restart Timer Service
Write-Log -info "Attempting to Start SharePoint 2010 Timer Service, if not
running." -logReq $true
Start-Service "SPTimerV4"
$spTimerService = Get-Service "SPTimerV4"
while($spTimerService.Status -ne "Running")
{
    Start-Sleep -Seconds 20
    Start-Service "SPTimerV4"
    $spTimerService = Get-Service "SPTimerV4"
}
Write-Log -info "SharePoint 2010 Timer Service is running now." -logReq $true

## start retract, delete, install and deploy solution.
if($Scope -eq "Global")
{
   DeleteSolution -solutionName $SolutionName
   DeploySolution -solutionName $SolutionName -solutionPath $SolutionsFolder
}
## IISReset
IISRESET
##Restart Timer Service
Write-Log -info "Attempting to Start SharePoint 2010 Timer Service, if not
running." -logReq $true
Start-Service "SPTimerV4"
$spTimerService = Get-Service "SPTimerV4"
while($spTimerService.Status -ne "Running")
{
    Start-Sleep -Seconds 20
    Start-Service "SPTimerV4"
    $spTimerService = Get-Service "SPTimerV4"
}
Write-Log -info "SharePoint 2010 Timer Service is running now." -logReq $true
Write-Log -info "DeploySolution Script Execution completed at $(get-date -format
MM-dd-yyyy-HH:mm:ss) ...." -logReq $true

Wednesday, 31 August 2011

SharePoint DataForm WebPart

DataForm WebPart
1. Open the visual studio.
2. Open the wspbuilder blank webpart solution.
3. Create a class file mentioned in the below image.














 4. Add the reference of micorsoft.Sharepoint, System.xml and System.web as shown in the above image.
5. Create a class file with name "YourClassFileName" but in this case DataForm_webpart.cs
6. Add the Below mentioned code into your file.
---------------------------------------------------------------------------------
using System;
using System.Web;
using System.Xml;
using System.Text;
using System.Web.UI;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace DataFormWebPart1
{
    public class DataForm_WebPart : DataFormWebPart
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Fetch_Data();
        }
        private void Fetch_Data()
        {
            StringBuilder strSpCentre = new StringBuilder();
            SPSite MySite = SPContext.Current.Site;
            SPList list = MySite.RootWeb.GetList("/Lists/yourlistname");
            XmlDocument doc = new XmlDocument();
            XmlNode queryResponse = doc.AppendChild(doc.CreateElement("dsQueryResponse"));
            XmlNode root = queryResponse.AppendChild(doc.CreateElement("Rows"));
            XmlElement rowNode;
            XmlAttribute att;
            if (list != null)
            {
                foreach (SPListItem item in list.Items)
                {
                    rowNode = doc.CreateElement("Category");
                    att = doc.CreateAttribute("Title");
                    att.Value = (string)item["Title"];
                    rowNode.Attributes.Append(att);
                    root.AppendChild(rowNode);
                    att = doc.CreateAttribute("URL");
                    att.Value = (string)item["Site Url"];
                    rowNode.Attributes.Append(att);
                    root.AppendChild(rowNode);
                }
               
            }
            XmlDataSource source = new XmlDataSource();
            source.CacheExpirationPolicy = DataSourceCacheExpiry.Absolute;
            source.CacheDuration = 1;
            source.Data = doc.InnerXml;
            source.ID = "MyTestWebPArts";
            this.XslLink = "/Style%20Library/XSL%20Style%20Sheets/DataForm_WebPart_Test.xsl";
            this.DataSource = source;
           
        }
        public override void DataBind()
        {
            base.DataBind();
        }
    }
}
-------------------------------------------------------------------------------------
 7. Build the solution. Create wsp file and deploy this wsp file into you webapplication by using central admin.
8. Now open SharePoint 2007 designer here we will create .xsl file which is reffering in the webpart code.
9. Open SharePoint Designer and Create one YourFileName.xsl which is reffering in the above webpart code. Please see below.











10. In  my example i have created with DataForm_Webpart_Test.xsl this is i am reffering in my webpart code.
11.Insert the below mentioned code into your xsl file.
------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet
  version="1.0"
  exclude-result-prefixes="x d ddwrt xsl msxsl"
  xmlns:x="http://www.w3.org/2001/XMLSchema"
  xmlns:d="http://schemas.microsoft.com/sharepoint/dsp"
  xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxsl="urn:schemas-microsoft-com:xslt">
 <xsl:output method="html" indent="no"/>
 <xsl:decimal-format NaN=""/>
 <xsl:template match="/" xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" xmlns:asp="http://schemas.microsoft.com/ASPNET/20" xmlns:__designer="http://schemas.microsoft.com/WebParts/v2/DataView/designer" xmlns:SharePoint="Microsoft.SharePoint.WebControls">
  <xsl:call-template name="dvt_1"/>
 </xsl:template>
 <xsl:template name="dvt_1">
  <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Category"></xsl:variable>
  <xsl:for-each select="$Rows">
   <a href="{@URL}">
    <xsl:value-of disable-output-escaping="yes" select="@Title">
    </xsl:value-of>
   </a>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>
------------------------------------------------------------------------
 12. Save this xsl file and CheckIn and Publish.
13. Pull your webpart from the webpart gallery and put on the page.
14. Now make the changes in the .xsl file according to your requirement.
15 You can do the formating in the .xsl file without touching your webpart.cs file. just open your .xsl file, checkout the file before making any changes. checkin the file and publish with major version. and refresh your webpart page. you will see the changes accrodingly.
16. Please Learn the xslt Syntax from w3schools.com. It will help you to understand the syntax of the xslt.
17. this is a very help full webpart. you can make a very rich UI. according to your requirement with out impacting to the .cs file.
18. In my case my .xsl file is placed in the style library of the site. you can place this file anywhere and just refer the path accordingly.
19. In this example i am reading a list and just displaying the title containing the list item as a link.

Tuesday, 19 July 2011

How to create SharePoint Content Type?
Sharepoint Content Type- Content Type is a re-usable collection of meta data (columns).
- For Creating SharePoint Content Type.Please follow the steps mention below :

1. Create a solution using visual studio use (wspbuilder solution template) in the below mentioned heirarchy.


2. Please create folder heirarchy mentioned in the above image.
eg :- 12-> TEMPLATE->FEATURES->SHAREPOINT.CONTENTTYPES.GLOBAL (YOUR FEATURE NAME)

3. Create three "xml" files mentioned in the above pic.

3.a feature.xml
3.b Fields.xml
3.c ContentType.xml

---------------------------------------------------------------------
Definining feature.xml
------------------------------------------------------------------

defining fields.xml



-----------------------------------------------------------------------

Defining ContentType.xml

3. After completion of the above task. Right Click on the solution, click on wspbuilder and click on Build wsp as soon in the below image.


4. Once it is done it will create a .wsp (windows solution package) file in to your solution directory as soon below


5. Add this solution package to the central admin using "stsadm" command.

ex :- stsadm -o addsolution -filename "SharePoint.Content-Type.wsp"


6. After adding the solution you need to deploy the solution through central admin. this will deploy the solution globaly. After deploying the solution. Open your sharepoint site and try to activate the feature (which we have created in the ablove solution) it will create the content type at the site level.

Once it is done you can use this content type with your list.