Create Multiple Document Sets from List - powershell

I need a document set create for every name in this SharePoint 2010 list. The Document set title is the first and last name of the person and the document set has committee name property that needs to be set as well. The committee name comes from the same SharePoint list.
I don't understand wrong with my code:
$ErrorActionPreference = "Stop"
$url = "http://SERVER/etest"
$listName = "Advisory Committee"
$doclib = "TACM Application Docments"
$web = Get-SPWeb $url;
$list = $web.Lists[$listName];
$item = $list.Items;
$item | ForEach-Object {
$fullName = $_['Last Name'] + ", " + $_['First Name']
$committeeName = $_['Committee Name']
$cType = $list.ContentTypes["Document Set"]
[Hashtable]$docsetProperties = #{"Committee Name"=$committeeName}
$newDocumentSet = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::Create($doclib.RootFolder,
$fullName,$cType.Id, $docsetProperties)
}
$web.Dispose()
I get the following error:
ForEach-Object : Cannot find an overload for "Create" and the argument count: "4".
At C:\Users\ev\desktop\docset.ps1:10 char:23
+ $item | ForEach-Object <<<< {
+ CategoryInfo : NotSpecified: (:) [ForEach-Object], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest,Microsoft.PowerShell
.Commands.ForEachObjectCommand

DocumentSet.Create method expects the first parameter to be a folder (SPFolder type):
parentFolder
Type: Microsoft.SharePoint.SPFolder
The folder in which to create the new DocumentSet object.
In your case you are passing an invalid object $doclib.RootFolder since $doclib is a string variable, probably you want something like this:
$doclibName = "TACM Application Docments"
$doclib = $web.Lists[$doclibName];
$newDocumentSet = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::Create($doclib.RootFolder,$fullName,$cType.Id, $docsetProperties)
Example
$url = "http://contoso.intranet.com"
$listTitle = "Documents"
$web = Get-SPWeb $url;
$list = $web.Lists[$listTitle]
$docSetContentType = $list.ContentTypes["Document Set"]
[Hashtable]$docSetProperties = #{}
$docSetName = "Archive"
$docSet = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::Create($list.RootFolder,$docSetName,$docSetContentType.Id, $docSetProperties)
$web.Dispose()
How to add Document Set content type into library
Go to Library Settings, then click Advanced Settings
set Allow management of content types? to Yesand click Ok button
click Add from existing site content types link and select
Document Set content type, then click Ok button

Related

Copy list item to another subsite list with powershell in SharePoint 2013

I tried to copy the list from root to subsite using PowerShell like the code below, but when in execute there is an error like the picture below. what is missing from my code
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
Set-ExecutionPolicy -ExecutionPolicy "Unrestricted" -Force
$site= new-object Microsoft.SharePoint.SPSite("http://Sharepointroot")
$web = $site.Openweb()
$listroot = $web.Lists["Meeting Room Book"]
$itemsroot = $listroot.items
$siteapps = Get-SPWeb -Identity "http://sharepointroot/Apps"
$listapps = $siteapps.Lists["Meeting Room History"]
$i = 0
foreach ($item in $itemsroot) {
$i++
write-host -foregroundcolor yellow $i
$newitem= $listapps.Items.Add()
$newitem["Title"] = $item["Title"]
$newitem["Booking_x0020_Date"] = $item["Booking_x0020_Date"]
$newitem["End_x0020_Time"] = $item["End_x0020_Time"]
$newitem["Booked_x0020_By"] = $item["Booked_x0020_By"]
$newitem["Display"] = $item["Display"]
$newitem["Category"] = $item["Category"]
$newitem["Status"] = $item["Status"]
$newitem["Recurrence"] = $item["Recurrence"]
$newitem["Meeting_x0020_Title"] = $item["Meeting_x0020_Title"]
$newitem["BB"] = $item["BB"]
$newitem.update()
}
$web.dispose
$site.dispose
start-sleep 10
and I have error like below.
You cannot call a method on a null-valued expression.
at line:5 char:1
+ $newitem= $listapps.Items.Add()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+CategoryInfo : InvalidOperation: (:) [],RuntimeException
+FullyUalifiedErrorID : NullArray
Edit:
for this case closed because of an error in me, the name of the list that is made is not the same. thank you for responding to my question.
for code copy list to another subsite its work
To my understanding, this error means the script is attempting to do something, but another part of the script does not have any information to permit the first part of the script to work properly. In this case, $TargetItem .

Change Credential Retrieval in SSRS with powershell

I want to change SSRS data source credential retrieval from using the following credentials to without any credentials with powershell and script fails.
I want to change value 'Store' to 'None' according to this article:
https://learn.microsoft.com/en-us/dotnet/api/reportservice2010.credentialretrievalenum?view=sqlserver-2016#ReportService2010_CredentialRetrievalEnum_None
This is my code:
$uri ='http://ServerName/ReportServer/ReportService2010.asmx?wsdl'
$reporting = New-WebServiceProxy -uri $uri -UseDefaultCredential -namespace "ReportingWebService"
$DataSources = $reporting.ListChildren('/', $true) | Where-Object {$_.Name -eq "DataSourceName"}
foreach($Object in $DataSources) {
$dataSource =$reporting.GetDataSourceContents($Object.path)
#$dataSource.CredentialRetrieval="None"
$dataSource.CredentialRetrieval=[ReportingWebService.CredentialRetrievalEnum]::None
$reporting.SetDataSourceContents($Object.path,$dataSource)
}
This is the error:
Exception calling "SetDataSourceContents" with "2" argument(s): "The combination of values for the fields UserName and CredentialRetrieval are not valid. --->
Microsoft.ReportingServices.Diagnostics.Utilities.InvalidElementCombinationException: The combination of values for the fields UserName and CredentialRetrieval are not valid."
At line:13 char:4
+ $reporting.SetDataSourceContents($Object.path,$dataSource)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SoapException
The issue was that i actually did not change existent credential retrieval settings 'Store' with required Username parameter.
To resolve it i should create new data source definition with new credential retrieval settings and apply it to my data source:
$uri ='http://servername/ReportServer/ReportService2010.asmx?wsdl'
$reporting = New-WebServiceProxy -uri $uri -UseDefaultCredential
$type=$reporting.GetType().Namespace
$DataSources = $reporting.ListChildren('/', $true) | Where-Object {$_.Name -eq "Data source name"}
foreach($Object in $DataSources) {
$dataSource =$reporting.GetDataSourceContents($Object.path)
$dataSourceDefinitionType = ($type + '.DataSourceDefinition');
$dataSourceDefinition = New-Object ($dataSourceDefinitionType);
$dataSourceDefinition.Extension = $dataSource.Extension; #get data from existent data source definition
$dataSourceDefinition.ConnectString = $dataSource.ConnectString #get data from existent data source definition
$credentialRetrievalDataType = ($type + '.CredentialRetrievalEnum');
$credentialRetrieval = new-object ($credentialRetrievalDataType);
$credentialRetrieval.value__ = 3;
$dataSourceDefinition.CredentialRetrieval = $credentialRetrieval;
$dataSourceDefinition.WindowsCredentials = $dataSource.WindowsCredentials; #get data from existent data source definition
$dataSourceDefinition.Enabled = $dataSource.Enabled; #get data from existent data source definition
$dataSourceDefinition.EnabledSpecified = $dataSource.EnabledSpecified; #get data from existent data source definition
$reporting.SetDataSourceContents($Object.path,$dataSourceDefinition)
}

Powershell script can't find URL

I have a powershell that kicks off a workflow in SharePoint but my system is not allowing me to run it. The error I'm getting is as follows:
Get-SPWeb: Cannot find an SPWeb object with id or URL: xxx.com and site Url xxx.com
At C:\cert.ps1:1 char:17
+ $web = Get-SPWeb <<<< -Identity "xxx.com"
+ CategoryInfo : InvalidData (Microsoft.Share....SPCmdletGetWeb:SPCmdletGetWeb) [Get-SPWeb], SPCmdletPipeBindException
+ FullyQualifiedErrorId : Microsoft.SharePoint.Powershell.SPCmdletGetWeb
I tried adding Add-PSSnapin Microsoft.Sharepoint.Powershell but I the get an error saying its already been added.
Here's the script:
$web = Get-SPWeb -Identity "xxx.com"
$manager = $web.Site.WorkFlowManager
$list = $web.Lists["Certificate Tracking"]
$assoc = $list.WorkflowAssociations.GetAssociationByName("Certificate Notification","en-US")
$view = $list.Views["All Items"] #All Items
$items = $list.GetItems($view)
$data = $assoc.AssociationData
foreach ($item in $items) {
$wf = $manager.StartWorkFlow($item,$assoc,$data)
}
$web.Dispose()
The -Identity parameter for Get-SPWeb can be either a full or relative path, or a path with a wildcard * character. Additionally:
The identity param expects a valid URL in the form http://server_name or a relative path in the form of /SubSites/MySubSite.
Try this instead using the -Site param, which I think might be what you're looking for:
Get-SPWeb -Site http://sitename/sites/site1

Exception calling "StartWorkflow" with "3" argument(s): ""

I am trying to write a powershell script that kicks off two workflows.
One needs to be kicked off only once while the other needs to be run on every item in the list. However I am being plagued by an error and I don't know how to get rid of it. When I add a 4 argument I get the same error. I am running the script as an spshelladmin
Error
Exception calling "StartWorkflow" with "3" argument(s): ""
At C:\cert.ps1:26 char: 29
+ $em = $manager.StartWorkFlow <<<< ($items[0],$emails,$data)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
Code
$web = Get-SPWeb -Identity "http://portal.com/sites/it"
$manager = $web.Site.WorkFlowManager
$list = $web.Lists["Certificate Tracking"]
$assoc = $list.WorkflowAssociations.GetAssociationByName("Certificate Notification","en-US")
$assoc.AllowAsyncManualStart = $true
$assoc.AllowManual = $true
$emails = $list.WorkflowAssociations.GetAssociationByName("Status Update","en-US")
$emails.AllowAsyncManualStart = $true
$emails.AllowManual = $true
$view = $list.Views["All Items"] #All Items
$items = $list.GetItems($view)
$data = $assoc.AssociationData
$emData = $emails.AssociationData
$count = 0
foreach ($item in $items) {
$wf = $manager.StartWorkFlow($item,$assoc,$data)
}
$em = $manager.StartWorkFlow($items[0],$emails,$emData,$true)
$web.Dispose()
I actually discovered that a specific item I was targeting already had a workflow running on it, I did not place proper checks for the status (check out the comments on this code http://paulryan.com.au/2014/cancel-all-workflows/), very misleading error, I thought I had wrong overload with method call on StartWorkFlow ($item, $association, $data, $true), in SP 2013 there are 4 parameters not sure why the thread title says three... Anyways hope that helps someone.

Copy Items Between Two EWS Servers - Powershell

I'm Trying to Get Into EWS, my Goal is to copy Data between Two Exchange Mailbox's in two different locations.
I'm Binding Two Inbox Folders, each in diffrent mailbox
Then i Get the items in the Source inbox folder and try to copy this items to the destination inbox folder, here's the code.
Add-Type -Path "C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll"
## Remote Connection ##
$MailboxName="User#Domain.com"
$ExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1
$Credentials = New-Object Microsoft.Exchange.WebServices.Data.WebCredentials("User","Password","Domain")
$exchService = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($ExchangeVersion)
$exchService.Credentials = $Credentials
$exchService.Url = "https://Domain.com/EWS/Exchange.asmx"
## Bind Remote Inbox Folder
$FolderName="Inbox"
$folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::$FolderName,$MailboxName)
$MailFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchservice,$folderid)
## Local Connection ##
$LocalMailboxName = "LocalUser#Domain.local"
$LocalExchangeVersion = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2
$LocalExchService = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($LocalExchangeVersion)
$LocalExchService.UseDefaultCredentials = $true
$LocalExchService.AutodiscoverUrl($LocalMailboxName)
$LocalExchService.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $LocalMailboxName)
$LocalFolderName="Inbox"
$Localfolderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::$LocalFolderName,$LocalMailboxName)
$LocalMailFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($LocalExchService,$Localfolderid)
## Get the items of the source mailbox "Inbox"
$ivItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(100)
$fiItems = $exchservice.FindItems($MailFolder.id,$ivItemView)
## The Copy Section
foreach ($item in $fiItems)
{
$item.Copy($LocalMailFolder.Id)
}
Then I get This Error:
Exception calling "Copy" with "1" argument(s): "No mailbox with such guid."
At line:1 char:30
+ foreach ($item in $fiItems) {$item.Copy($LocalMailFolder.Id)}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ServiceResponseException
Appreciate any help, Thanks
You cannot use copy to copy mails between Exchange Services, the error simply means that the remote service does not know the local folder id - because that folder(with that ID) does not exist on the remote service.
You need to load the item, create a NEW item in your local folder, copy all properties from the old item to the new one, then save the new item.
As I do not know Powershell, here a C# listing that should get you started, instead of:
foreach ($item in $fiItems)
{
$item.Copy($LocalMailFolder.Id)
}
->
foreach (var remoteItem in fiItems)
{
var localItem = new Item(LocalExchService);
localItem.Subject = remoteItem.Subject;
localItem.Body = remoteItem.Body;
// ... and so on. need to cast to the correct type, to access ALL properties, too.
localItem.Save(LocalMailFolder);
}