SharePoint Nirvana | Powershell

Trapping Errors when Automating Metalogix Content Matrix via Powershell

by JamieMcAllister 23. December 2016 04:11
Using Powershell to automate Metalogix Content Matrix and migrate content can be very powerful. One problem though is that the way to trap errors is undocumented. I chip in the necessary information here. [More]

Tags: , , ,

Content Matrix | Powershell

Deleting SharePoint Items remotely via Powershell

by JamieMcAllister 16. December 2016 03:27
Recently I was trying to put together a really quick solution to delete some SharePoint 2010 items remotely using Powershell script. It'd be quite usual to incorporate CSOM into such a solution, but as I had some legacy powershell performing actions via the SharePoint web services (lists.asmx) it seemed like I could hack it together quicker that way. How wrong I was! To cut a long story short it turned out that the MSDN documentation on this is wrong! This led to head scratching and a much slower resolution than I was after. The MSDN documentation is here; Some wrong MSDN Docco This tells you that to delete multiple list items you need to build a package for the web service call like this; <Batch OnError="Continue" ListVersion="1" ViewName="270C0508-A54F-4387-8AD0-49686D685EB2"> <Method ID="1" Cmd="Delete"> <Field Name='ID'>2</Field> </Method> <Method ID="2" Cmd="Delete"> <Field Name='ID'>8</Field> </Method> </Batch> See how it just requires the ID values? WRONG! It turns out it needs the FileRef attribute too otherwise you're in a world of pain; <Batch OnError="Continue" ListVersion="1" ViewName="270C0508-A54F-4387-8AD0-49686D685EB2"> <Method ID="1" Cmd="Delete"> <Field Name='ID'>2</Field><Field Name="fileRef">http://domain/sites/TheSite/TheLibrary/The File 223.pdf</Field> </Method> <Method ID="2" Cmd="Delete"> <Field Name='ID'>8</Field><Field Name="fileRef">http://domain/sites/TheSite/TheLibrary/The File 991.pdf</Field> </Method> </Batch>   So assuming you've put the above XML together in your powershell, how do you invoke the delete via web services? Like this; $batchElement = [The XML from above] $SiteName = "http://MyDomain/sites/TheSite" $listName = "TheLibrary" $ServiceDesc = $SiteName + "/_vti_bin/lists.asmx?WSDL" $service = New-WebServiceProxy -UseDefaultCredential -uri $ServiceDesc $results = $service.UpdateListItems($listName, $batchElement) You can then check your $results XML for success codes (or other!) Happy SharePointing!


CSOM | Powershell | Sharepoint | sharepoint 2010

Powershell Find Items with no checked in version

by JamieMcAllister 17. April 2014 11:33
When a user adds a document to a library for the first time, but omits to check in that file that information risks getting lost. This can happen for a variety of reasons such as missing mandatory fields or simple forgetfulness. The user who added the file can see it fine, and won't know that anything is wrong. Unfortunately, until it's checked in for the first time other users and even the Site Collection Admin can't see it. Since SharePoint is all about sharing information, this is bad. If you're a site owner you can manage these files. Document Library settings has an option called 'Manage files which have no checked in version'. This shows you which files have the issue, and allows you to take control of the files so they can be checked in. There's a few issues with this alas; You can't sort or filter the list of files You can't export the list except a clumsy copy and paste of the page HTML Someone has to remember to check for 'lost' files No way to notify users automatically Luckily, there's an easy way to get the list of 'lost' items via the API. So I set about creating an automatic notification. The following powershell calls the CheckedOutFiles property of the library, puts the results into a CSV and emails it to the support folks so they can gently explain to users what happened and correct it. Actually though as you'll see from the documentation here there are several properties and methods to play with. You could easily use the CheckedOutByEmail property to email users directly, or the TakeOverCheckOut method to checkin the file sautomatically instead. I saw some commentary online stating that the CheckedOutFiles property only yields results when the library has Force Checkout enabled. As far as I can tell this is rubbish as my library doesn't have force checkout and the script works fine. To deploy this I placed it in a secure folder on the server and used Task Scheduler to execute it once a week. Simple and perfect! Script below. Happy SharePointing!   $ErrorActionPreference = "Stop" $ver = $host | select version if($Ver.version.major -gt 1) {$Host.Runspace.ThreadOptions = "ReuseThread"} if(!(Get-PSSnapin Microsoft.SharePoint.PowerShell -ea 0)) { Write-Progress -Activity "Loading Modules" -Status "Loading Microsoft.SharePoint.PowerShell" Add-PSSnapin Microsoft.SharePoint.PowerShell } $SourceWebURL = "http://MyDomain/sites/ProposalDocs" $SourceLibraryTitle = "Proposal Documents" $file = 'C:\Powershell\QATools\CheckedOutFiles.csv' $sWeb = Get-SPWeb $SourceWebURL $sList = $sWeb.Lists | ? {$_.Title -eq $SourceLibraryTitle} $InvisibleFiles = $sList.CheckedOutFiles foreach ($InvisibleFile in $InvisibleFiles) { $out = "$($InvisibleFile.CheckedOutBy),$($InvisibleFile.Url)" $out | Add-Content $file } $smtpserver="" $smtp = new-object Net.Mail.SmtpClient($smtpServer) $att = new-object Net.Mail.Attachment($file) $msg = new-object Net.Mail.MailMessage $msg.To.Add("") $msg.To.Add("") $msg.From = "" $msg.Subject = "Checked Out Items" $msg.Body = "Attached is the Checked Out Files report." $smtp=new-object Net.Mail.SmtpClient($smtpServer) $msg.Attachments.Add($att) $smtp.Send($msg) $att.Dispose() Remove-Item $file



Create Multiple Document Sets with Powershell

by JamieMcAllister 9. April 2014 09:47
A quick example of how to create multiple Document Sets in SharePoint driven by a CSV file for the names and properties. Changing a couple of lines would allow this script to create simple Folders instead (but Document Sets are better!). [More]

Tags: , , ,

Powershell | Sharepoint | sharepoint 2010 | SharePoint 2013

Disclaimer: The software, source code and guidance on this website is provided "AS IS"
with no warranties of any kind. The entire risk arising out of the use or
performance of the software and source code is with you.

Any views expressed in this blog are those of the individual and may not necessarily reflect the views of any organization the individual may be affiliated with.