All posts tagged Visual Studio

SPLongOperation – Explained & Branded

Processing

Overview

When you are performing out of the box operations in SharePoint 2010 normally in Central Administration you will get a nice loading screen from SharePoint with an animated loading gif.  Have you ever wondered how you can use this in your own SharePoint Solution?  I have been doing SharePoint for a while now and only recently discovered the SPLongOperation method and it is quite the little gem.

SPLongOperation.BeginOperation beginOperation = null;

if (beginOperation == null)
{
    beginOperation = delegate(SPLongOperation longOperation)
    {
        // Long running code here ..

        longOperation.End("settings.aspx", SPRedirectFlags.RelativeToLayoutsPage, HttpContext.Current, null);
    };
}
SPLongOperation.Begin(beginOperation);

I have been writing a site provisioning service recently and have used the above code to help provide the end users some feedback while a new site collection is being created.  Only a couple of lines of code but it transformed the solution.

Want to change the text that is displayed in the loading screen?  No problem.

// BEGIN SPLongOperation
SPLongOperation longoperation = new SPLongOperation(this.Page);
longoperation.LeadingHTML = "<div><h2>This is the main Title</h2></div>";
longoperation.TrailingHTML = "<div><h3>This is the second line of text underneath</h3></div>";
longoperation.Begin();

// long running code here

// END SPLongOperation
longoperation.End(Customers.DefaultViewUrl, Microsoft.SharePoint.Utilities.SPRedirectFlags.Default, this.Context, "");

This will change the text on the page to reflect your specific requirements.

Branding the Loading Page

Do you want to match the loading page to your corporate colour scheme?  No problem.  Below is the output of the page in raw html.

<body onload="setGearPageFocus();gotoNextPage();" class="s4-simple-gearpage">
	<div id="GearPage">
		<div id="s4-simple-card" class="s4-simple-gearpage">
			<div id="s4-simple-card-content">
				<h1>
					<a id="gearsImageLink" href="javascript:;" onclick="hideGears();" title="This animation indicates the operation is in progress. Click to remove this animated image." >
						<img id="gearsImage" alt="This animation indicates the operation is in progress. Click to remove this animated image." src="/_layouts/images/gears_anv4.gif" style="width:24px; height:24px; font-size:0px;" align="absmiddle" />
					</a>
					Processing...
				</h1>
				<div>
					Life In SharePoint Long Operation Demo
				</div>
				<div>
					<br /><br />This is a custom long operation text
				</div>
			</div>
		</div>
	</div>
</body>

As you can see the markup is very simple.  What you can then do is create a new CSS style sheet and inject the reference to it in the LeadingHTML property as the property is not escaped.

longoperation.LeadingHTML = "<link rel='stylesheet' href='/_layouts/LifeInSharePoint/LongOperation.css' type='text/css'  />Life In SharePoint Long Operation Demo";

This will then enable you to customize the page as you need to changing background colours, fonts images etc.  This is the kind of result that you will get.

Do download a working solution with branding included please use the link below.

LISP.LongOperationDemo.zip

Visual Studio 2010 – SharePoint (There are no Content Types in the Project)

I came across this error when trying to create a new list definition based on a content type that i have defined in my project.

I was so confused as to why this suddenly started happening, i thought i may have been when i placed my solution into source control, however after 5 hours of testing, re-writing the code again thinking that my project was broken it suddenly clicked what had happened.  Basically you cannot place a SharePoint Project into a Solution Folder.  Doing this causes Visual Studio to loose its ability to locate your SPI’s in your project.  Removing the project from the folder and back into the root of the solution fixed the problem.

I hope that this helps people save many hours of lost time like i did.

Chris