All posts in PowerShell

How to Delete a SharePoint List Item Attachment with PowerShell

Every now and then a problem pops up in that a user is unable to delete an attachment on a list item.  The system goes through the motions of deleting the item, but when you get back to the list item the attachment is still there.  No errors are received and no reason is given for the item not deleting.  I haven’t seen this happen very often but it happened recently in one of our team site lists.  There was nothing special about the list item or the attachment, but for whatever reason, it wouldn’t delete the attachment.  Like so many things I come across I figure if it happened to me, it could happen to someone else.  So that brought about this blog post.  I want to show you how to delete a list item’s attachment with PowerShell.

How to Delete a SharePoint List Item Attachment with PowerShell

So to start off I have a simple list with an item that has two attachments.  I want to delete the one called “AutoSPInstallerFolderStructure.txt”.  The process for deleting it is pretty straightforward.  You simply access the list, get the item we are looking for in the list and then loop through the attachments until we find the one we want to delete and remove it.

Building the script:

Before the Script:

How to Delete a SharePoint List Item Attachment with PowerShell - BeforeScript

Running the Script:

How to Delete a SharePoint List Item Attachment with PowerShell - Running The Script

After the Script:

How to Delete a SharePoint List Item Attachment with PowerShell - After the Script

And that’s it.  You can also modify the script to delete attachments in bulk if the need is there.

 

Thanks for reading!

SharePoint Saturday Vancouver 2017

This week I had the distinct privilege to present at SharePoint Saturday Vancouver.  I presented a search based session with Joanne Klein and a new presentation I just finished about using  PowerShell to make your day-to-day administration\usage of SharePoint so much easier.  As always, I had a great time presenting.  Had great interaction with the attendees of my sessions.

A huge thank you to the sponsors and to the Vancouver SharePoint Users Group.

As promised here are the slide decks for the two presentations:

Finding the Needle in a Haystack SharePoint Style: Search Concepts and Content Quality

Stop Clicking Your Mouse Button, We have a Script for That!

Start SharePoint 2013 Workflows with PowerShell

Recently was working on a small project that ended up requiring me to start SharePoint 2013 workflows with PowerShell scripts.  This turned out to be a necessity because of a few things.  I was finishing off some development recently on a smaller project that required a significant amount of JavaScript injection code.  To be terribly honest, had I known at the time it would have taken as much as it did, I would have rewritten it as a full Add-In, but that’s a story for another day.  To make a long story short because of the way the form had been written by another developer we were saving attachments to the list manually from the form (not allowing SharePoint to do it for us).  Because we had workflows running on item added, we were having save conflicts because the items could not be added until after the form data was initially inserted into the list.  Basically getting a kind of race condition between the workflows and the list item update.

My first thought was to simply disable the workflows from starting automatically and instead initiating them from the form using JSOM.  This however wasn’t going to work because while the code I wrote worked perfectly fine (I’ll blog on that later), I forgot the solution also had a mobile app feeding into it.  The mobile app wasn’t able to use the same model as I could and SharePoint 2013 workflows is one of the few places REST needs to be vastly improved.  In theory it can be done simply by using the ExecuteProcess endpoint and enter the necessary information in the body of the transmission.  However, I had never done this and didn’t have a lot of time left to figure it out (I’ll post something here if I get around to working it out).  So that left a PowerShell Script and scheduled tasks.

Start SharePoint 2013 Workflows with PowerShell

So now let’s get to why you’re here.  How to initiate SharePoint 2013 workflows from PowerShell.  The gist of my solution was this:

  1. Build a search query for every item created in the past 10 minutes (scheduled task will run every 5 min so there will be plenty of overlap).
  2. Loop through each item returned and check to see if the workflows have run based on the workflow status field created.
  3. If no workflow for that item, initiate the workflow.

Search Query

The query is pretty straight forward:

The script only takes in the number of minutes you want to look into the past.  You get the current date and time, back it up the number of minutes you want, convert it to ISO 8601 format and build it into your query to find any items later than that time.  If you aren’t up on CAML query, the code basically translates to “Get all items, where the field ‘Created’ (of type DateTime) is greater than or equal (<geq>) to the date I want in ISO 8601 format.

Initiate the Workflow

When initiating the workflow there are a couple of steps you need to do first:

  1. Connect to the SharePoint Workflow Services and create a Workflow Services Manager object
  2. Connect to the subscription service
  3. Get the workflow subscription you are looking for (searching an enumerated list using the workflow name)
    • You are checking all the workflow associations (definitions) within the site and grabbing the one you are looking for
  4. Build an empty dictionary.  This is used to hold the parameters of the workflow if there was an initiate form gathering information.
  5. Creating an object of the workflow instance service and initiating the workflow against the item in question.

That’s it.  These few lines, find the workflow you need, create an instance of it against the item you wish and then start the workflow.

You next step is to build it all together and then toss it into a scheduled task to run every 5 minutes or however often you need it to run.

Thanks for reading!

Quickly Build a Multi-Server SharePoint 2013 Developer Environment (Part 4) – Preparing SharePoint Servers

Welcome to part four of my series on how to “Quickly Build a Multi-Server SharePoint 2013 Developer Environment”.  In this section I am going discuss how to configure SharePoint servers and prepare our last two VMs.  We’ll be using a series of tools to accomplish this, including AutoSPSourceBuilder and AutoSPInstaller (which if you followed the prep from part two you already have downloaded and ready to go).  The other thing we are going to do that has been different than the steps done so far is we are going to configure both SP servers and then install SharePoint and configure the farm all at once.  To do this we will be using the remote installation option that AutoSPInstaller gives us.

Posts in this series:

Read more

Make Hidden SharePoint Fields Visible Again

There are times when you set a field to visible (optional or required) in the GUI, yet it refuses to show up on the list\library.  This seems to occur if your content type inherits from a parent that has the field hidden.  If you attempt to make the child content type field visible again it doesn’t seem to cascade down into the list instance of the content type and thus your field never shows up on the list.

So you think, “ok, I’ll just use PowerShell”.  You’re on the right track, but there is more to it than meets the eye.

if you run the following code:

You are going to get the following error message:

Exception setting “Hidden”: “Cannot change Hidden attribute for this field”

ErrorMessage

What you don’t realize is that when this particular problem occurs, there is an internal field that you can see in the field xml, but is not part of the regular properties called CanToggleHidden.  As it sounds, the purpose of this field is to ensure the field can’t be enabled.

But wait!!  There’s hope.

There’s a method that can be used called SetFieldBoolValue.  It allows us to modify that value.  If you do a little research on it, the method is listed in technet.  But it’s one of those: “It’s there but you shouldn’t use it” kind of methods.  Well in this case we have to use it.  The only way to get to it is via the Reflection namespace as follows:

Once you have completed the script you will find your field is visible again.

Thanks for reading!