Set Permission to List Items using PowerShell


Add-PSSnapin Microsoft.SharePoint.Powershell

$url = "http://weburl"
$listName = "List Name";
$permissionLevel = "Permission Level Name";
$groupName = "Enter Group Name";

#Grant permission on all uniquely secured list items to the specified group

$web = Get-SPWeb $url;
$list = $web.Lists[$listName];
$permission = $web.RoleDefinitions[$permissionLevel];
$principal = $web.SiteGroups[$groupName];

#Process each list item

foreach ($item in $list.Items) {
Write-Output ("Item #" + $item.ID.ToString());

#Check to see whether the item is uniquely secured

if ($item.HasUniqueRoleAssignments -eq $FALSE) {
Write-Output " No change, permissions are inherited.";
}

else {

#Find an existing role assignment for this principal

$assignments = $item.RoleAssignments;
$assignment = $assignments | where {$_.Member.Name -eq $principal.Name};
if ($assignment -eq $NULL) {

#Add a new role assignment for the principal

$assignment = new-object Microsoft.SharePoint.SPRoleAssignment($principal);
$assignment.RoleDefinitionBindings.Add($permission);
$assignments.Add($assignment);
Write-Output (" Granted " + $permissionLevel + " to " + $groupName);
}

elseif ($assignment.RoleDefinitionBindings.Contains($permission) -ne $TRUE) {

#Update the principal's role assignment to add the desired permission level

$assignment.RoleDefinitionBindings.Add($permission);
$assignment.Update();
Write-Output (" Updated " + $groupName + " permissions to " + $permissionLevel);
}
else {
Write-Output " No change.";
}
}

}
$web.Dispose();

Advertisements

Migrate data from Source to Destination Library without changing the audit trail column values– PowerShell

Add-PSSnapin "Microsoft.SharePoint.Powershell" -ErrorAction SilentlyContinue

$sourceWebURL = "http://srcurl"
$sourceListName = "Source"

$destinationWebURL = "http://desurl"
$destinationListName = "Destination"

$spSourceWeb = Get-SPWeb $sourceWebURL
$spDestinationWeb = Get-SPWeb $destinationWebURL

$spSourceList = $spSourceWeb.Lists[$sourceListName]
$spDestinationList = $spDestinationWeb.Lists[$destinationListName]

$RootFolder = $spDestinationList.RootFolder

$spSourceItems = $spSourceList.Items

ForEach ($item in $spSourceItems)
{

Try
{

$binary = $item.File.OpenBinary();

if ($binary -ne $null)
{
$sBytes = $item.File.OpenBinary()

[Microsoft.SharePoint.SPFile]$spFile = $RootFolder.Files.Add($item.Name, $sBytes, $true)
$theItem = $spFile.Item
write-host -f Green "...Success!"
$pos = $item["Author"].IndexOf("#")
$userAuthorLogin = "amat\"+$item["Author"].Substring($pos+1)

$pos1 = $item["Editor"].IndexOf("#")
$userEditorLogin = "amat\"+$item["Editor"].Substring($pos+1)

$dateCreatedToStore = Get-Date $item["Created"]
$dateModifiedToStore = Get-Date $item["Modified"]

$userAuthor = Get-SPUser -Web $spDestinationWeb | ? {$_.userlogin -eq $userAuthorLogin}
$userAuthorString = "{0};#{1}" -f $userAuthor.ID, $userAuthor.UserLogin.Tostring()

$userEditor = Get-SPUser -Web $spDestinationWeb | ? {$_.userlogin -eq $userEditorLogin}
$userEditorString = "{0};#{1}" -f $userEditor.ID, $userEditor.UserLogin.Tostring()

#Sets the created by field
$theItem["Author"] = $userAuthorString
$theItem["Created"] = $dateCreatedToStore

#Set the modified by values
$theItem["Editor"] = $userEditorString
$theItem["Modified"] = $dateModifiedToStore

#Store changes without overwriting the existing Modified details.
$theItem.UpdateOverwriteVersion()

write-host -f Green "...Success!"

}

}
Catch [system.exception]
{
write-host "Caught a system exception for " $item.ID $item.Title
}
Finally
{
$spSourceWeb.Dispose();
$spDestinationWeb.Dispose();
}

}

Migrate data from CSV to SharePoint List – PowerShell


Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue

$FilePathCSV = import-csv D:\Book1.csv

$webURL = "http://url"
$listName = "CSVInput"
$web = Get-SPWeb $webURL
$list = $web.Lists[$listName]

foreach($CurrentRwo in $FilePathCSV)
{
$field1 = $CurrentRwo.Name
$field2 = $CurrentRwo.Age

$newItem = $list.Items.Add()
$newItem["Title"] = $field1
$newItem["Age"] = $field2

$filebytes = [System.IO.File]::ReadAllBytes("D:\Book1.csv")
$newItem.Attachments.Add("DummyFile Title",$filebytes)
$newItem.Update()
}

 

How to read the response details from the discussion board list using Powershell script?

Requirement 

I have a SharePoint 2010 Discussion Board and it has many Discussions and Responses.

Customer asked me to read the responses entered for a particular discussion topic from the discussion board using Powershell script.

Solution

I have used the below Powershell script to do this.

cls
if((Get-PSSnapin | Where {$_.Name -eq “Microsoft.SharePoint.PowerShell”}) -eq $null)
{
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}

$web = Get-SPWeb “http://server/sites/JRDW”
$listName = $web.GetList(“http://server/sites/JRDW/Lists/TestDiscuss/”)
#Get desired discussion topic by Name Or Run a for each by removing the where class
$discussionTopics = $listName.Folders | Where-Object {$_.Name -eq “Test 1″}

#Get all discussion messages
$caml='<Where><Eq><FieldRef Name=”ParentFolderId” /><Value Type=”Integer”>{0}</Value></Eq></Where>’ -f $discussionTopics.ID
$query = new-object Microsoft.SharePoint.SPQuery
$query.Query = $caml
$query.ViewAttributes = “Scope=’Recursive'”;
$sourceMessages = $listName.GetItems($query)

foreach ($sourceMessage in $sourceMessages) {

Write-Host $sourceMessage[“Body”];
Write-Host $sourceMessage[“TrimmedBody”];
Write-Host $sourceMessage[“Author”];
Write-Host $sourceMessage[“Editor”];
Write-Host $sourceMessage[“Modified”];
Write-Host $sourceMessage[“Created”];

}