Upload file in SharePoint Style Library using Powershell

This Powershell code snippet upload xsl file into style library.

 

 

if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PsSnapin Microsoft.SharePoint.PowerShell
}

#Script settings
$webUrl = “http://MKS”

function UploadFile( [String] $FilePath)
{
#Open web and library
$web = Get-SPWeb $webUrl

$file =Get-Item $FilePath
write-host “Started Uploading File……”$file.FullName

#Open file
$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

$folder =  $web.getfolder(“Style Library”)

$web.GetFile($folder.Url + “/XSL Style Sheets/” + $file.Name).CheckOut()

$spFile = $folder.Files.Add($folder.Url + “/XSL Style Sheets/” + $file.Name, [System.IO.Stream]$fileStream, $true)

$spFile.CheckIn(“Checkin by Powershell”)
$spFile.Publish(“Published by Powershell”)

write-host $webUrl”/”$folder.Url”/XSL Style Sheets/”$file.Name

write-host “Successfully Copyied file ” $file.Name

$fileStream.Close();

$web.Dispose()
}

UploadFile -FilePath “C:\***.xsl”

Advertisements

Feature receiver not working when activating through the powershell script

Sometime while deploying package using powershell script it works fine and also activates
the feature, but any feature receiver event does not fire.

To solve this issue:-

Try to close the powershell command window and open new powershell  window.
and then run the deployment script once again and this worked for me.

SharePoint 2010 FAST Search : Property doesn’t exist or is used in a manner inconsistent with schema settings

In order to fix error Property doesn’t exist or is used in a manner inconsistent with schema settings ,

1. Double check the data-type of any newly created Managed Property with respect to the crawled property.

2. Start full crawl the content source and verify if search started working properly.

3. In case search result still shows the same error, then run the following command in FAST Powershell ,  followed by full crawl, read.

                                        nctrl restart qrserver

 

 

How to create a Lookup Field From Sub-Site List to Root-Site Collection List (Cross-Site Lookup)

This solution is based on power shell script for creating cross site look up field.

For example there is a sub site http://mksportal/site1 which contains a list “Product Name”  and
there is another sub site http://mksportal/site2/site3.
In order to make cross site look up field available to sub site through content type,
this can be achieved by using power shell script.

There are couple of things mandatory, when cross site look up is created on that time both sub site and list must exist because it is dependent on list ID.
This is basic script, one can customize further like add reference field for the same lookup field mapped to other field like ID etc.
try
{

$mksWebSite =  “http://mksPortal/Site1”

$spweb = Get-SPWeb $mksWebSite
$webids=$spweb.ID

$lst = $spweb.Lists[“Product Name”]

$fieldXML = ‘
<Field ID=”GUID”
Name=”mksField”
Group=”MKS
Type=”Lookup”
DisplayName=”mksField”
StaticName=”mksField”
List=”{‘ + $lst.ID +’}”
ShowField=”Title”
Overwrite=”TRUE”
WebId=”‘ + $webids + ‘”  ></Field>’

$field = $spweb.Site.RootWeb.Fields.AddFieldAsXml($fieldXML)
$fieldRef = $spweb.Site.RootWeb.Fields.AddFieldAsXml($mksFIELDRef)
$spweb.Site.RootWeb.Update()
$ctypeParent = $spweb.Site.RootWeb.availablecontenttypes[“My Custo CType”]
$newContentType = $spweb.Site.RootWeb.ContentTypes[$ctypeParent.ID];
$fieldLink = new-object Microsoft.SharePoint.SPFieldLink($spweb.Site.RootWeb.Fields[ $fieldInternalName])
$newContentType.FieldLinks.Add($fieldLink)
$newContentType.Update()

$spweb.Dispose()

}

catch [System.SystemException]

{

write-host “Execution stopped due to: “+$_.Message -foregroundcolor Red

}

PowerShell Command for SharePoint 2010

1.  Install / Uninstall and Activate/Deactivate Feature

$Hive14=’C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES\’

$Url=’http://devsp:9901&#8242;
$Feature=’MKS.Services.ProdDev.Images’
$Path=’E:\WorkArea\MasterPage\’ + $Feature
$DestPath=$Hive14 + $Feature

Remove-Item $DestPath -recurse

Copy-Item $Path  $DestPath  -recurse
Disable-SPFeature $Feature -Url $Url
Uninstall-SPFeature $Feature
Install-SPFeature $Feature
Enable-SPFeature $Feature -Url $Url

2. Configuring MasterPage and CSS
$newLogoUrl=’http://devsp:9901/PublishingImages/_t/MukeshMainLogo_jpg.jpg&#8217;
$web = Get-SPWeb http://devsp:9901
$web.MasterUrl = “/_catalogs/masterpage/MKSProductDefaultMasterPage.master”
$web.CustomMasterUrl= “/_catalogs/masterpage/MKSProductDefaultMasterPage.master”
$web.AlternateCssUrl=”/_LAYOUTS/1033/STYLES/CUI.css”
$web.SiteLogoUrl = $newLogoUrl
$web.Update()
$web.Dispose()