Tuesday, 31 December 2013

Delete custom permission level using PowerShell


Delete custom permission level using PowerShell

cls

$spSite = Get-SPSite "http://sedwdevrtm:22222/sites/a123 "


$spWeb = $spSite | Get-SPWeb

# check to see if the permission your are going to delete is not existing..

if($spWeb.RoleDefinitions["MyCustomPermission"] -ne $null)
{
    $spRoleDefinition = New-Object Microsoft.SharePoint.SPRoleDefinition
    $spRoleDefinition.Name = "MyCustomPermission"
    $spWeb.RoleDefinitions.Delete( $spRoleDefinition.Name)
     Write-Host $spRoleDefinition.Name " Deleted successfully"
}
else
{
Write-Host "Custom Permission doesn't exist"
}
$spWeb.Dispose()

$spSite.Dispose()




Add Custom Permission Levels using PowerShell


Add Custom Permission Levels using PowerShell 


$spSite = Get-SPSite "http://abc/sites/xyz"
  
$spWeb = $spSite | Get-SPWeb

# we add a new Permission Level name "MyCustomPermission" to the site, which will allow users to only add new items (no editing or removing)

# check to see if the permission your are going to add is already existing.. if null proceed

if($spWeb.RoleDefinitions["MyCustomPermission"] -eq $null)
{
    $spRoleDefinition = New-Object Microsoft.SharePoint.SPRoleDefinition
    $spRoleDefinition.Name = "MyCustomPermission"
    $spRoleDefinition.Description = "Can only Add items"
    $spRoleDefinition.BasePermissions = "ViewListItems, AddListItems, Open, ViewPages"
    $spWeb.RoleDefinitions.Add($spRoleDefinition)
}

$spWeb.Dispose()

$spSite.Dispose()



Note : Below command will list , all the base permissions options in the 


[System.Enum]::GetNames("Microsoft.SharePoint.SPBasePermissions")




Thursday, 26 December 2013

PowerShell Script to read SiteUrl's from Text file

Create text file fore example : SiteUrls.txt

http://sedwdevrtm:22222/sites/abc
http://sedwdevrtm:22222/sites/bcd
http://sedwdevrtm:22222/sites/def
http://sedwdevrtm:22222/sites/xyz

PowerShell Script to read SiteUrl's from Text file


 clear

# Read the text file by using " Get-Content " Powershell Command

$data=Get-Content "C:\SiteUrls.txt"

 Write-Host "Site count" is  $data.Count

 #Loop through each line in text file using foreach and display using Write-host

foreach($d in $data)
    {
   
     Write-Host  -BackgroundColor DarkYellow $d

    } 



Output :




Create document library's in SharePoint using Power Shell with xml data as input

Create document library using Power Shell with xml data as input

We had a business requirement to automate Creating of Same document library's, Folders and fields inside that  in more 110 sites which will be repeated for year.


Hence we created an xml file with all the document library names and folder names and fields inside that .


Powershell script will read the xml file contents and based on that we are creating the folder structure in document library.

PowerShell Script :

    clear

       # Add the Powershell snapin, 
      
       if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
       {
           Add-PSSnapin "Microsoft.SharePoint.PowerShell"
       }

 # Load the SharePoint Assembly
      
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

 # Read the Site Url from User

    $url = Read-host " Enter Name of the Site to Add Document Library"

    $listTemplate =  [Microsoft.SharePoint.SPListTemplateType]::DocumentLibrary

 # xml file contains list of documentlibrary's , folders, fields to be created in the site


    [xml]$file = Get-Content "C:\XmlDocumentLibFolderStructure.xml"

       function CreateDocLibs()
       {
             #Check if the site exists
            
               $OpenWeb = Get-SPWeb $url -ErrorVariable err -ErrorAction SilentlyContinue -AssignmentCollection $assignmentCollection
              
                    if($err)
                           {
                           write-host " Site is Not Exisiting Kindly contact Administrator"
                           }
                    else
                           {
                           write-Host "Site " $url " exists"
                           }

             foreach( $ls in $file.Structure.Lists.List)
             {
                    $doclibname=$ls.ListName  
                    # Creating Document Library     
                    Write-Progress " Please wait writing Document Library"  $ls.ListName
                  $checkDoclib=$OpenWeb.Lists[$doclibname]
                     
                     # Check if the Document is null i.e document libary is not created.

                     if($checkDoclib -eq $null)
                           {
                           $OpenWeb.Lists.Add($doclibname,"",$listTemplate)           
                           Write-Progress "Created Document Library successfully" 
                           Write-Progress "Creating Fields" 
                           $lib=$OpenWeb.Lists[$doclibname]
                           Write-Host $lib.Url
                           $lib.EnableVersioning = $true
                           Write-Host "Document library with " $doclibname " Created"
                           Write-Progress " Please wait Adding Fields to " $doclibname
                           $fldXml = "<Field Type='MultiChoice' DisplayName='Document Status' Name='DocumentStatus'  >
                                                  <CHOICES>
                                                                <CHOICE>Review Required</CHOICE>
                                                                <CHOICE>Review in Progress</CHOICE>
                                                                <CHOICE>Review Completed</CHOICE>
                                                              </CHOICES>
                                                     </Field>"
                           $lib.Fields.AddFieldAsXml($fldXml,$true,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)
                           $lib.Update()
                           $spFieldTypeDocNum =[Microsoft.SharePoint.SPFieldType]::Text
                           $lib.Fields.Add("Doc No",$spFieldTypeDocNum,$false)
                           $lib.Update()
                          
                           # get the folder option value from " XmlDocumentLibFolderStructure.xml"
                          
                           Write-Progress "Creating Folder Structure ........ Please wait..."
                           sleep 1
                           $foldername = $file.Structure.FolderOptions.Folder
                                 foreach($folder in $ls.FolderOption)
                                 {
                                     
                                     $values = $foldername | where {$_.option -eq $folder}
                                     $splitvalues= $values."#text" -split ","
                                        foreach($i in $splitvalues)
                                        {
                                        $folder1 =  $lib.AddItem("",[Microsoft.SharePoint.SPFileSystemObjectType]::Folder,$i)
                                        $folder1.Update()
                                        }
                                 }
                           }
                          
                           # If Document Library is Already Existing skip it
                           else
                           {
                           Write-Host "Document Library al
                           ready Exists with " $doclibname " can not be overrided"
                           }
             }
       }


CreateDocLibs


============================


XML File :


<Structure>

<FolderOptions>
<Folder Option="1">2014 01,2014 02,2014 03,2014 04,2014 05,2014 06,2014 07,2014 08,2014 09,2014 10,2014 11,2014 12</Folder>
<Folder Option="2">2014 03,2014 06,2014 09,2014 12</Folder>
<Folder Option="3">2014</Folder>
</FolderOptions>
<Lists>
<List>
<ListName>2.1 Statements 2014</ListName>
<FolderOption >1</FolderOption>
<Fields>
<Field Type="Text" Name="Doc No"></Field>
<Field Type="Choice" Name="Document Status"></Field>
</Fields>
</List>
<List>
<ListName>2.2 Other Documents 2014</ListName>
<FolderOption>1</FolderOption>
<Fields>
<Field Type="Text" Name="Doc No"></Field>
<Field Type="Choice" Name="Document Status"></Field>
</Fields>
</List>
<List>
<ListName>3.1 Account Reconciliations 2014</ListName>
<FolderOption>1</FolderOption>
<Fields>
<Field Type="Text" Name="Doc No"></Field>
<Field Type="Choice" Name="Document Status"></Field>
</Fields>
</List>
<List>
<ListName>3.2 BS Open Items Reports 2014</ListName>
<FolderOption>1</FolderOption>
<Fields>
<Field Type="Text" Name="Doc No"></Field>
<Field Type="Choice" Name="Document Status"></Field>
</Fields>
</List>
<List>
<ListName>3.3 Other Closing Material 2014</ListName>
<FolderOption>1</FolderOption>
<Fields>
<Field Type="Text" Name="Doc No"></Field>
<Field Type="Choice" Name="Document Status"></Field>
</Fields>
</List>
<List>
<ListName>3.4 Sign-Off Package 2014</ListName>
<FolderOption>1</FolderOption>
<Fields>
<Field Type="Text" Name="Doc No"></Field>
<Field Type="Choice" Name="Document Status"></Field>
</Fields>
</List>
<List>
<ListName>5.2 FA - Fixed Asset Sales 2014</ListName>
<FolderOption>1</FolderOption>
<Fields>
<Field Type="Text" Name="Doc No"></Field>
<Field Type="Choice" Name="Document Status"></Field>
</Fields>
</List>
</Lists>
</Structure>

Output :