Posts Tagged ‘SharePoint’

Useful SharePoint script to restart SharePoint services and IIS

Friday, December 2nd, 2011

A SharePoint developer’s life is filled with many things, but one of the most common is the old faithful iisreset, coupled with a reset of the Timer Service and the Admin Service. When you’re working on timer jobs, it can get tedious quickly to have to  keep resetting things.

So I’m sharing a useful batch script I knocked together to do it for you. It will perform three functions – restart the SharePoint 2010 Timer Service, the SharePoint 2010 Administration Service and do an IISReset. It will prompt you if you want to do each of them, and if you don’t reply within 5 seconds automatically do it.

Simply save it as a .cmd file, and then create a shortcut to it, and put that shortcut on your start menu/quick launch. Restarts ahoy!

Enjoy.

@echo off
@echo Quick SharePoint services restart - from www.thorntontechnical.com
@echo Stopping Sharepoint services...

:P1
REM - SharePoint timer service
CHOICE /C:YN /M "Restart Timer Service?" %1 /T 5 /D Y

if ERRORLEVEL ==2 GOTO P2
if ERRORLEVEL ==1 GOTO A1
:A1
net stop "sharepoint 2010 timer"
net start "sharepoint 2010 timer"
:P2
REM - SharePoint timer service
CHOICE /C:YN /M "Restart Admin Service?" %1 /T 5 /D Y
if ERRORLEVEL ==2 GOTO P3
if ERRORLEVEL ==1 GOTO A2
:A2
net stop "sharepoint 2010 administration"
net start "sharepoint 2010 administration"
:P3
REM - reset IIS
CHOICE /C:YN /M "Reset IIS?" %1 /T 5 /D Y
if ERRORLEVEL ==2 GOTO END
if ERRORLEVEL ==1 GOTO A3
:A3
iisreset -restart -noforce
:END
echo Finished. Thanks. Have fun.

The field with Id {} defined in feature {} was found in the current site collection or in a subsite

Tuesday, November 1st, 2011

This is a known bug in Visual Studio. It seems to occur most often when using VS to deploy (and redeploy) declarative content types to a SharePoint site. I’ve seen it happen most often when deploying List Definitions and List Instances. There are a variety of steps out there to try to make it go away:

1. Retract solution, close VS, restart VS, deploy solution.
2. Deploy project, attempt to activate via UI, deactivate, retract, restart VS, deploy solution.
3. Deploy, retract, deploy. Open Task Manager, kill VSSHostP4.

Deploying after this should be resolved.

Error activating features. Field ” could not be found, it may have been deleted by another user.

This is not a bug, more of an irritation. If you’re deploying List Instances using Visual Studio, you may see this if you create your List Definition/Content Type/Fields, but then forget to add the Fields to your List Schema. At the top of the Schema, you can supply a

ContentTypeRef

(the ID of your Content Type) and then in the Fields collection, essentially copy your Field definitions in.

Other deployment related issues

One other issue you may encounter is in the ordering of items in your Feature/Package. It might seem obvious but you must ensure things are ordered in the way that they will be needed during deployment. That is,

  • Fields
  • Base content types
  • Content types that inherit base content types
  • Anything that uses the content types and fields, e.g., Document Set Content Type. Note: if your Document Set is using a custom Welcome Page (deployed via a module/element), this must be deployed first
  • List definitions
  • List instances
  • Anything else

You can arrange the deployment order in the Feature designer screen. But be warned- if you’ve got a lot of Elements in your project (and multiple projects in a solution), then it’s a pretty gnarly process.

Other deployment pointers? Leave a comment below.

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…)