Posts Tagged ‘SharePoint’

Quick tip: SPListItem.CopyTo custom method

Friday, October 28th, 2011

One use for my custom Ribbon buttons could be to move files (or Document Sets) from one library to another. I was trying to get the SPListItem.CopyTo method working, but for various reasons, it refused to play ball.

I came across a handy piece of code that is essentially a custom method for doing the same thing. Couldn’t understand a word of the article, but got the gist of what the code was doing. It essentially creates an item in the target location, and then copies all the field info over to the new item. This works fine for Lists, and even copies attachments, but what about for document libraries, which are setup a bit differently?

This code achieves the same. What you’re able to do is at the time you create the file, use a Hashtable full of all the metadata you want to apply to your new item.

  private void CopyItem(SPListItem sourceItem, string destinationListName)
        {
            Hashtable metadata = new Hashtable();
            foreach (SPField f in sourceItem.Fields)
            {
                if (!f.ReadOnlyField && f.InternalName != "Attachments")
                {
                    metadata.Add(f.InternalName, sourceItem[f.InternalName]);
                }
            }

            //copy sourceItem to destinationList
            SPList destinationList = sourceItem.Web.Lists[destinationListName];
            SPFile targetItem = sourceItem.Web.Files.Add(destinationList.RootFolder + "/" + sourceItem.Name, sourceItem.File.OpenBinary(), metadata);

        }

Enjoy.

SharePoint 2010 + Document Sets + Custom Ribbon Buttons with Custom Code

Thursday, October 27th, 2011

I recently posted a guide on how to add an item to the SharePoint 2010 context menu (“EditControlBlock”) and run some custom code on the click action. SharePoint 2010 uses the love-it-or-hate-it Microsoft Ribbon, which, to the developer, is fully accessible. This guide will show you how to implement a custom button for a Document Set, in the Manage group and also on the Edit form, and run some custom code when it’s clicked, in Visual Studio 2010.
(more…)

Don’t hack the SharePoint 2010 database

Thursday, October 6th, 2011

The Channel Islands recently suffered from a massive power failure. Well, in fact, it was 2.5 power failures in the space of three days (one on the Monday, again on Wednesday around 7pm, then again at about 9pm, about 5 minutes after the power had been restored.)

One of my clients suffered from a fairly extensive failure as a result – a couple of disks in their SAN were lost. This had serious consequences for the SQL Server that was hosting some SharePoint databases on top of it. We experienced lots of page corruptions, and, long story short, the SharePoint content database was left in a mangled state. And to make matters worse, in a “perfect storm” of both independent sets of backups from the previous night were also lost. (How this could have been allowed to happen is a topic for another day.) After exhausting all known options, we were left with a situation where we had a backup which was about 2 weeks old and half a database from which to try and recover some of the recent data. We then broke the biggest SharePoint rule: never go anywhere near the SharePoint database.
(more…)

SharePoint 2010 context menu item with custom code

Wednesday, September 28th, 2011

List items in SharePoint lists and document libraries have a “context menu” on the Filename/FileLeafRef field. When you hover to the right of the field, you see a drop down menu with a bunch of options. You can add your own items to this list, and also run custom code when the item is clicked. This tutorial will show you how, using Visual Studio 2010 and SharePoint 2010.

Edit: 16/02/2012 – there were a few comments about the custom assembly not firing. I’ve revised the post below to include an extra bit in the XML to ensure the control loads (see #13.)

(more…)

More fun with deploying Document Sets via XML

Thursday, September 15th, 2011

To finish a set of posts about the fun I’ve had in deploying document sets as XML content types, here’s a fun little bug vagary I encountered. In the video walkthrough, there’s a section on deploying the custom WelcomePage to your site, and provisioning some WebParts automatically to that page.

You achieve this in the Elements.xml file of your WelcomePage module by including some <AllUsersWebPart>> sections. Within those tags, you enter the definition of the webpart, and you can reuse the built-in webparts.

(Hint: to get at the required definition for built-in SP webparts, find the webpart in the Webparts Gallery in Site Settings, save a copy of the file to your hard drive (as a .dwp file) and then open that file in Visual Studio. You get the required info. Neat.)

You can then deploy your feature, and assuming you’ve covered all the bases your Document Sets, with custom Welcome Page should be deployed. But here’s a problem. Let’s say you make some changes to it, and redeploy the feature. You may experience one of a series of behaviours:

1) None of your changes are displayed
2) Your changes are displayed OK
3) You end up with multiple webparts on your custom Welcome Page.

It’s the third issue I tripped over, and it seems to occur if/when you manually deactivate and reactivate your feature (using the “deploy” feature from VS doesn’t seem to cause this.) It makes some sense – the Elements.xml file says “add these webparts to this page”. Assuming you’ve not added any FeatureDeactivating event receiver to remove the webparts when the feature is deactivated, well, you’ll end up with lots of webparts on the one page.

This being the case, how do you actually go about getting rid of the extra webparts on the page? Retracting the solution doesn’t help. You could completely bin your site content type, site or even site collection. That would probably do it. But it’s a bit drastic. The alternative? Update the database manually. You can decide if this is more drastic than the previous options.

To be clear, you should never, ever, on any account, ever, manually hack the SharePoint content database!

But if you want to do it, here’s how:

(more…)