SharePoint Nirvana | sharepoint 2010

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

SharePoint 403s and the PermissionMask Mystery

by JamieMcAllister 4. May 2015 11:11
Getting 403's from your SharePoint farm, or PermissionMask errors in ULS? This is what caused it for me. [More]


sharepoint 2010

Print Multiple Documents from SharePoint

by JamieMcAllister 24. March 2015 09:50
I got a query from a colleague who monthly needs to print many documents from a folder in a document library. This was taking them a lot of time, and they asked me to enable 'something' to print all the documents at once. There are commercial products to achieve this, but for tens of documents there's a free workaround. Here are my documents in O365 (though this is identical in 2013, and 2010); Simply click on the venerable 'Open with Explorer' option; In explorer you can select up to fifteen documents at once, and a print button remains visible. This cuts down the work of multi-document printing a lot! The documents can't be mixed, so this doesn't work for e.g. PDFs and Word together, but you can sort on the Type column to get everything into one batch. Happy SharePointing!


Office 365 | sharepoint 2010 | SharePoint 2013 | SharePoint Online

Add Search for Current Document Set

by JamieMcAllister 4. March 2015 10:14
  Document Sets were first introduced in SharePoint 2010 and are an excellent way to organize content. I've gained many enthusiastic users of Doc Sets, but there is one feature they often ask for - the ability to search inside a particular Document Set. The issue is that Document Sets can easily end up with tens or hundreds of documents in them, but the default search scope in SharePoint is usually going to search the whole site. With this in mind I devised a solution to allow my users to search just in the Document Set they're currently looking at. Wouldn't it be nice to allow users to search inside the current Document Set like this? It just takes a few minutes to put this together! (I've documented here for SharePoint 2010, but this also works for 2013 and Office 365 with a minor tweak detailed below)   One very nice feature of Document Sets is that they have a 'Homepage' that you can edit. This allows you to change the look and feel, including adding extra web parts and through use of the Content Editor Web Part add Script. This thinking led me to my solution... Search results webparts in SharePoint are driven by querystring parameters in the url of the page. I therefore realized that if I added a Core Search Results webpart to the Document Set homepage, and manipulated the URL with the users search term and the Document Set address then I could show Document Set specific Search results right there in the Document Set page. The concept is illustrated below. I need to take the url of the Doc Set, and add two querystring parameters; k for the search term and u for the container I wanted to search. (This is Microsoft's naming convention, not mine!)     To achieve the above I first need to change the Doc Set Homepage for my library. Go to library settings and click the Document Set Content Type. (This is assuming you've already added that!)     In the next page click Document Set Settings     In that next page you'll see an option to edit the Doc Set Welcome Page.     Now you see the default template for the welcome page     This is what that same page looks like when you click edit. The various web part zones are visible;     Upload the Javascript file (SearchAndFilter.js) I've attached at the end of the article to your local Assets Library. We're going to reference that file in a Content Editor Web Part (CEWP). Or you could take the text from that javascript file and paste into the HTML view of the CEWP, but that's not good practise. Now we add a CEWP to the page as shown;     In the Web Part settings, set the content link to be the javascript file you just uploaded to the Assets Library;     Next add a Search Core Results web part to the same web part zone;     Your page now looks like this;     Your users now see a Search box when they're viewing Document Sets. Entering a Search Term and clicking they see results from just that Document Set as below. This is better than before but the results are kind of ugly.     To change the look of the Search results to be friendlier, edit page again and go into the Search Core Results web part settings. Expand Display Properties, and click the XSL Editor button. (You may need to untick the 'Use Location Visualization' box to ungrey the button). In the editor that pops open, delete the text and paste in the contents of my SearchStyling.xsl file, also at the bottom of this article. Click OK and save your changes.     Your search results should now look fairly like a normal List View. Sorting and filtering is possible on the search columns, and the description field which is sometimes verbose can be expanded or not. The XSL I've provided can be tweaked to display different columns (there's a lot of properties in search results that aren't shown by default) including your own custom columns.     The files you need to make this work are below;   SearchAndFilter.js (3.60 kb) [SP2010] SearchStyling.xsl (10.91 kb) [SP2010] SearchAndFilterO365.js (3.60 kb) [SP2013 or Office 365]   If you need this for Office 365 or SharePoint 2013 instead, there's a one line tweak needed to the javascript. The Search Results webpart in that version puts it's 'search results not found' text in a different bit of the page. (Also you'll style your Search Results with Display Templates instead, which is off topic here.)   Happy SharePointing!


Document Management | Javascript | Office 365 | sharepoint 2010 | SharePoint 2013 | SharePoint Online

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

How to Write SharePoint Code for Bad Performance

by JamieMcAllister 31. January 2014 08:53
Whilst browsing some SharePoint blogs I came across an interesting code sample. The code showed how to edit item properties in an Event Receiver on a library.Here's a partial excerpt; using (SPSite site = new SPSite(properties.Web.Site.ID)) {     using (SPWeb web = site.OpenWeb(properties.Web.ID))     {         SPList list = web.Lists[properties.List.ID];           SPListItem item = list.Items.GetItemById(properties.ListItemId);    This code caught my eye, because it was totally unnecessary to instantiate the SPSite and SPWeb objects to get a handle to the list item. Far better to just use event properties without instantiating anything! properties.ListItem  So, in for a penny, in for a pound I thought I'd compare the code performance. In a standalone dev environment with a few list items the bad code would run fine. I have access to a test environment with tens of thousands of items, which provided a better test of Production-like conditions. The good code executed in 30ms fairly consistently, according to the logs. Fair enough.  What I found was the bad code execution time started climbing as I fired more and more items into the list and triggered the event receiver more often. I gave up when the bad code was executing in 12000 milliseconds. Yup. In my sizable library, the bad code ran 400 times slower than the code using properties.ListItem!! This is why it's not good practice instantiating unnecessary SPSite and SPWeb objects. (So also consider SPContext.Current when you're not in Event Receivers for similar reasons). Also shows you why you shouldn't trust code samples on blogs without a bit of code review (including mine!). Happy SharePointing!


.NET | Development | Event Receiver | Sharepoint | sharepoint 2010 | SharePoint 2013

Hiding Multiple Ribbon Buttons

by JamieMcAllister 10. January 2014 10:38
For a particular Form Library I was interested in making it difficult to do too much with the Forms except add them. I employed a range of measures, one of which was to hide the ribbon buttons for Sharing or opening with Explorer View.  This can be done by creating a customAction and deploying it to your site. Some of this is documented elsewhere, but I noticed that there were no examples showing multiple buttons hiding - the examples showed a single instance. Also the examples always showed a "RegistrationId" of 101, without explaining that this targeted Document Libraries. To target Form Libraries I had to dig up the fact that "RegistrationId" of Form Libraries is 115. The xml looks like this; <?xmlversion="1.0"encoding="utf-8"?> <Elementsxmlns="">   <CustomAction     Id="RemoveRibbonButton"     Location="CommandUI.Ribbon"     RegistrationId="115"     RegistrationType="List">     <CommandUIExtension>       <CommandUIDefinitions>         <CommandUIDefinitionLocation="Ribbon.Library.Actions.OpenWithExplorer" />         <CommandUIDefinitionLocation="Ribbon.Library.Actions.ExportToSpreadsheet" />         <CommandUIDefinitionLocation="Ribbon.Library.Actions.OpenWithAccess" />         <CommandUIDefinitionLocation="Ribbon.Library.Share.ViewRSSFeed" />         <CommandUIDefinitionLocation="Ribbon.Library.Share.AlertMe" />         <CommandUIDefinitionLocation="Ribbon.Library.Share.EmailLibraryLink" />         <CommandUIDefinitionLocation="Ribbon.Library.Actions.TakeOfflineToClient" />         <CommandUIDefinitionLocation="Ribbon.Library.Actions.ConnectToClient" />       </CommandUIDefinitions>     </CommandUIExtension>   </CustomAction>   </Elements>   To get this into your site, create an empty SharePoint project in Visual Studio. A sandboxed solution is fine. Add a feature to the project, and paste the above XML into the Elements.xml file. Smallest scope for the feature is Web. Build and add the WSP to your Solution Gallery via Site Collection Settings. When activated, and the Feature activated in a given Web, it will hide the named buttons in all Form Libraries in the current SPWeb. Here's a list of other elements you could hide using the above method; Mike Smith kindly documented the many Registration IDs for different libraries here; Happy SharePointing!

Tags: ,

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

Strange Bug - A View that thinks it's a zip file

by JamieMcAllister 3. September 2013 03:19
I recently witnessed a strange error where a List View page became convinced it was a zip file. This stopped all users from accessing the library. Here's what I did about it. [More]


Bug | sharepoint 2010 | SharePoint Designer

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.