Wednesday 6 August 2014

GAC + sharepoint powershell




[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
//Add your dll folder
$publish.GacInstall("c:\temp\MyDLL.dll")
iisreset

Thursday 5 December 2013

Update document library in sharepoint through c#

using (SPSite psite = new SPSite(SPContext.Current.Site.Url))
                {
                    string sectionurl = SPContext.Current.Web.ServerRelativeUrl;
                    using (SPWeb pweb = psite.OpenWeb(sectionurl))
                    {
                        pweb.AllowUnsafeUpdates = true;
                        SPDocumentLibrary mylist = pweb.Lists[Constants.libraryname] as SPDocumentLibrary;
                        string fileName = System.IO.Path.GetFileName(FileUploadControl.PostedFile.FileName);

                        //Get file extension ( *.doc OR *.docx )

                        string fileExtension = FileUploadControl.FileName.Substring(FileUploadControl.FileName.IndexOf("."));
                        byte[] fileBytes = FileUploadControl.FileBytes;
                        string destUrl = mylist.RootFolder.Url + "/" + fileName;
                        SPFile destFile = mylist.RootFolder.Files.Add(destUrl, fileBytes, true);
                        destFile.Update();

                        //update metadata

                        destFile.Item["File Main Category"] =Convert.ToString( hndBatchID.Value);
                        destFile.Item["File Sub Category"] = Convert.ToString(hndBatchID.Value); ;
                        DateTime txtStartDate = DateTime.Parse(txtactstartdate.Text);
                        DateTime txtEndDate = DateTime.Parse(txtactenddate.Text);
                        destFile.Item["Act Start Date"] = txtStartDate;
                        destFile.Item["Act End Date"] = txtEndDate;
                        destFile.Item["File Description"] = txtactfiledesc.Text;
                        destFile.Item.Update();
                        lblmessage.Text = "Documents Added Successfully. ";
                    }
}

Powershell scripts in Sharepoint 2013

Add Solution

Add-SPSolution -LiteralPath C:\test.Application.wsp


Deploy Solution For all Web Applications


Install-SPSolution -Identity test.Application.wsp -AllWebApplications -GACDeployment -Force

Deploy Solution For Specific Web Applications

Install-SPSolution –Identity $wsp -GACDeployment -force -WebApplication "http://test.com"


Update WSP in Sharepoint

Update-SPSolution -Identity ListDefinition.wsp -LiteralPath "C:\Users\Desktop\published\ListDefinition.wsp"-GACDeployment

Backup site collection

Backup-SPSite http://test:11111/test -Path C:\Backup\testBack.bak

Enable the SharePoint Server Publishing Feature


Enable-SPFeature -Identity "PublishingWeb" -Url http://test\

Wednesday 4 December 2013

Create and Restore Site Collection along with Unique Content database and managed paths in Sharepoint using Powershell

Add-PSSnapin Microsoft.SharePoint.PowerShell –ErrorAction SilentlyContinue
$ErrorActionPreference = "Continue";

#Get input from user

$webapp = Read-Host "Enter WebApplication Name";
$site = Read-Host "Enter site Name";

#create managed paths

New-SPManagedPath $site -WebApplication "http://$webapp" -Explicit

Write-Host "Site Collection $site  has been created successfully"  -ForegroundColor Yellow


#Get input from user

$dbname = Read-Host "Enter Content Database Name";
$owner1 = Read-Host "Enter Content PrimaryOwner Name";
$owner2 = Read-Host "Enter Content SecondaryOwner Name";

#Default values

$server = "MY_CONTENT_DB_ALIAS";

#Create content db

Write-Host "Creating Content DB"
New-SPContentDatabase -Name $dbname  -WebApplication http://$webapp/ | out-null
Write-Host "Content DB Created Sucessfully"

Write-Host "Site Collection http://$webapp/$site"

#Create the site collection

Write-Host "Creating Site Collection $site"
New-SPSite -URL http://$webapp/$site -OwnerAlias $owner1 -SecondaryOwnerAlias $owner2 -ContentDatabase $dbname | out-null
Write-Host "Site Collection $site Created Sucessfully"

#Set the content db so it will only contain 1 site collection

Get-SPContentDatabase -Site http://$webapp/$site | Set-SPContentDatabase -MaxSiteCount 1 -WarningSiteCount 0
Write-Host "Set Content DB to only contain 1 Site Collection"


Write-Host "Site Collection at" $site "has been created in the" $dbname "content database" -ForegroundColor Yellow

#Restore sitecollection

Restore-SPSite http://$webapp/$site -Path C:\test.bak -Force

Write-Host "$site Site restored successfully"-ForegroundColor Red