SharePoint Nirvana | CSOM

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

Alternatives to SharePoint Timer Jobs

by JamieMcAllister 24. November 2013 04:49
Custom SharePoint Timer Jobs are a bad idea more often than not. Here are some alternative approaches to get the same results in a (hopefully) future-proofed and Cloud Friendly way. [More]

Tags: , , ,

.NET | CSOM | Development | Office 365 | On Prem | sharepoint 2010 | SharePoint 2013 | Timer Jobs | Workflow

Update Choice Field using CSOM (Client-side Object Model)

by JamieMcAllister 21. November 2013 11:59
 I was putting together some CSOM Managed code to update an existing Choice column in SharePoint. Noticed that existing online examples show creating a new column or reading the values in a column, but nothing about update. The Red Herring that is the SchemaXml property on Field sent me the wrong way for a few minutes. Finally realized that by using the CastTo method on the context I could get a FieldChoice instance rather than just a Field. Once you have that the Choices are defined as a string array so very easy to update or replace. Here's the code;   using (ClientContext ctx = new ClientContext("http://MyServer/MySite"))             {                 Field genericField = ctx.Web.Lists.GetById(listID).Fields.GetById(fieldGuid);                 FieldChoice fldChoice = ctx.CastTo<FieldChoice>(genericField);                 ctx.Load(genericField);                 fldChoice.Choices = “MyChoice1;MyChoice2;MyChoice3”.Split(";".ToCharArray());                 fldChoice.Update();                 ctx.ExecuteQuery();                              }

Tags: ,

.NET | CSOM | 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.