How to create a Search Box delegate in SharePoint
Simple but effective…I recently had to change the behaviour of the ‘SmallSearchBox’ control within SharePoint. I wanted to loose the ‘Advanced Search;’ link, the button style and also add some default text into the search box. I found the following method to be the simplest approach.
Standard Search Box
Custom delegate applied
The search box is rendered using a delegate control. If you look at the master page you will see the delegate …
<SharePoint:DelegateControl runat=”server” ControlId=” SmallSearchBox”>
You can alter the behaviour of a delegate control by specifying a delegate with a smaller sequence number. I created the following delegate control within a standard feature…
Feature…
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
< Control Id="SmallSearchInputBox" Sequence="10" ControlClass="Microsoft.SharePoint.Portal.WebControls.SearchBoxEx" ControlAssembly="Microsoft.SharePoint.Portal, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"> <
<Property Name="GoImageUrl">/_layouts/images/CFB/go.gif</Property>
<Property Name="GoImageUrlRTL">/_layouts/images/CFB/.gif</Property>
<Property Name="GoImageActiveUrl">/_layouts/images/CFB/go.gif</Property>
<Property Name="GoImageActiveUrlRTL">/_layouts/images/CFB/go.gif</Property>
<Property Name="UseSiteDefaults">true</Property>
<Property Name="FrameType">None</Property>
<Property Name="QueryPromptString">Search…</Property>
<Property Name="DropDownMode">HideDD_useDefaultScope</Property>
</Control>
</Elements>
Note the Sequence=”10” attribute of the Control element. WSS out of the box refers to a sequence number of 100 and MOSS overrides this with a sequence number of 50. So as long as your sequence number is lower than this then your settings will be applied.
The four changes I was concerned was
1. Remove the ‘advanced search’ link which is handled by the ‘ShowAdvancedSearch’ property,
2.Add some default text to the search box which is handcled by the QueryPromptString property
3. Remove the scope drop down field. The DropDownMode property with the enumuration value of HideDD_userDefaultScope sorts that one out.
4. Finally I changed the image to my own go.gif image. I also deployed this within the same feature to a sub directory called CFB within the 12 hive images folder.
For a complete listing of the available properties please refer to the following link on MSDN.
How to you describe SharePoint to your customers
If like me you find it impossible to describe SharePoint without at least 10 minutes, the use of 1,000 words and a whiteboard then you might find this video from Microsoft useful…
SharePoint 2010 Sneak Peek
Well at last we get can get a glimpse into some of the features coming out in SharePoint 2010, due for main release in the first half of next year. (PS No NDAs broken here, this stuff is all publically available from the Microsoft Web site
)
The Developer Dashboard
Check out the ‘Developer Dashboard’ where you can “review diagnostic information including detailed page request information such as timings, names and resources for all stored procedures calls, memory used and authenticated user, the number of SPRequests objects, any asserts and critical events and timings for web parts events for the page rendering” – and breath
LINQ for SharePoint
Use LINQ to access SharePoint lists,
joins and projections
Visual Studio 2010
Some amazing improvements to the development experience within Visual Studio. Life might just get a little simpler from now one?
SharePoint explorer
Cool, you no longer need to use SharePoint Manager 2007 (Although this is an excellent tool) You can now explore SharePoint directly from Visual Studio 2010
Visual Web Part Designer
You have to look at the visual web part designer. Design web parts just like web pages. Drag and drop those controls and away you go!
Business Connectivity Services – The new BDC!
So Read/Write capabilities are now included in the new BDC, that’s great and is going to be SO powerful!
…..check out the videos online here
Using the SharePoint dispose checker tool
Objects using IDisposable
A number of objects within the SharePoint object model implement the IDisposable interface. For the on-going performance of your SharePoint server you need to ensure you remember to correctly dispose of these objects within your code. I guess this has always been important for any application you are building but its a bit more crucial in SharePoint as your building your solutions on a platform that isn’t just servicing your application but could be many.
So what’s the problem?
So what if you don’t dispose of your objects? Well your just using up memory that could have been released. So without recycling your application pool you run the risk of slowly degrading the performance of your SharePoint farm.
In fact your application pools will automatically recycle when they hit a particular memory usage threshold, which could then result in slow page loads as pages are re-compiled by the JIT when requested.
How to spot the problem?
If your spotting your SharePoint server application pools recycling frequently or your servers performance degrades during heavy usage then this could be an indication that your custom\3rd party code\web parts are not correctly disposing their objects.
You can check the ULS logs at 12 hive \LOGS. Your looking for entries related to SPRequest. It might be complaining about the number of SPRequests objects exceeding a configurable threshold and so on. You can increase this threhold setting through the registry. But first, try and use this as an indicator that some of your code or indeed a 3rd partys code such as some web parts etc. are not correctly disposing the SPSite and SPWeb objects. So look at this first before cranking this level up.
If you do want to change the threshold then look for the following sub key in your registry using regedit…
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\HeapSettings
LocalSPRequestWarnCount = <set to the level you want>
How to dispose your objects
Ok so that the problem, and how to determine if your getting the problem covered. So how do you ensure your code isn’t leaving your SPSite and SPWeb objects ‘indisposed’. Well, as mentioned previously, both of these objects implement the IDisposable interface so you can go ahead and call the .Dispose method when your done with the object. Good practice would be to put this in a try, catch finally block…
SPSite site = null; try{ site = new SPSite("http://litwaredemo"); Console.WriteLine("got a handle on the site {0} : {1}", site.Title. site.Url); } catch(Exception ex) { //Catch exceptions here } finally { if (site != null) site.Dispose(); }
Another approach, which I prefer, is to use the using statement. This will ensure the dispose is taken care of without you worrying about it. Like so…
using (SPSite site = new SPSite(http://litwaredemo) { Console.WriteLine("got a handle on the site {0} : {1}, site.Title, site.Url); }
The one catch (if you’ll pardon the pun) to dispose, is to only take care of the objects that you have instantiated, if your grabbing a handle from things like SPContext then you don’t need to worry about disposing this, let SPContext take care of it.
SPSite site = SPContext.Site;Console.WriteLine("got a handle on {0} : {1}",
site.Title, site.Url);
//Don’t dispose site, leave this to SPContext to worry about.
Using the SharePoint Dispose Checker Tool
Microsoft have released a neat console application which you can add into Visual Studio and have check your code once your all done. It will output any suspect methods which may not be disposing these objects correctly. You need to look at the output as it may be reporting some false positives, for example you may have some factory methods which instantiate these objects and return them. But I find this tool invaluable!
You can download the link from MSDN here Dispose Checker Tool To add this into Visual Studio just…
1. select Tools\External Tools from the menu option in VS. Then click add.
2. Go ahead and give the tool a friendly name (include an & next to a one of the letters if you want to use a keyboard shortcut).
3. in the command box add the correct path to the checker executable
eg C:\Program Files\Microsoft\SharePoint Dispose Check\SPDisposeCheck.exe
4.In the Arguments box select the menu icon and select ‘Project Directory
5. Check the ‘Use output window’ to have the results kept within VS.
6. And finally, select ‘prompt for arguments’ this is useful if you need to add additional arguments such as –debug etc.
Then once your done writing your code, just compile as normal then run the tool. Below is an example output which is telling me I haven’t disposed of the xxSite object. I have highlighted the important bits in red…
| Line: 121Notes: Disposable type not disposed: Microsoft.SharePoint.SPSite ***This may be a false positive depending on how the type was created or if it is disposed outside the current scope
More Information: http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_110 ———————————————————- ID: SPDisposeCheckID_110 Module: LookingAtWebFields.exe Method: ConsoleApplication1.Program.Main(System.String[]) Statement: XXsite := new Microsoft.SharePoint.SPSite(“http://litwaredemo”) Source: C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\Lab Work\LookingAtWebFields\LookingAtWebFields\Program.cs Line: 121 Notes: Disposable type not disposed: Microsoft.SharePoint.SPSite ***This may be a false positive depending on how the type was created or if it is disposed outside the current scope More Information: http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_110 ———————————————————- Total Found: 2 ———————————————————- Modules Checked: 3 ———————————————————- LookingAtWebFields.exe LookingAtWebFields.vshost.exe LookingAtWebFields.exe ———————————————————- Modules Ignored: 0 ———————————————————- ———————————————————- Methods Ignored: 0 ——————————————————— |
So there you go, hope this document has been of some help. Happy SharePoint coding
Groove to become SharePoint Workspace 2010
Microsoft have recently announced that their Office Groove product shall be re-launched as SharePoint Workspace 2010. It’ll will be interesting to see what functionality this shall offer but I suspect this shall bring some real off-line and replication capabilities to SharePoint. Then we can rename SharePoint as Lotus Microsoft Notes
For more information you can subscribe to Microsoft’s new SharePoint workspace blog. I’ll let you discover the ‘snappy’ title they have given it : SharePoint Workspace Blog
SharePoint Manager 2007 – one for your tool box!
If you haven’t explored the tools and projects going on in Codeplex around SharePoint I would strongly encourage you to take a look. There is a wealth of great accelerators, ideas and useful tools that can save you a lot of time when developing SharePoint solutions. Codeplex – SharePoint
One of my ‘must have’ tools is SharePoint Manager 2007. This tool is a SharePoint object model explorer and enables you to browser every site on your local farm and view every property. You can even change the properties via the tool which can be a great time saver. Every spent time creating quick and nasty console apps to make some changes to your SP configuration. I know I have!
I also find this a great tool to quickly audit a new SharePoint farm and get a quick glimpse of the sites, app pools etc in use.
Branding application pages
One thing that is kind of a gotcha when it comes to rebranding your SharePoint sites is application pages. If you have just rebranded your web site either by creating new CSS styles sheets and wiring these to the existing default.master or indeed created your own master page any application pages won’t be rendered using your new styling. Why?…
Application pages live in the 12 hive ‘layouts’ directory and is available to ALL sites in your farm via a virtual directory called _layouts. Application pages don’t use the master pages specified within the site, they use their own master page called application.master and this too lives in the layouts directory.
If you go branding the application.master page directly then this is going to change and effect all your sites in your farm. A better approach would be to determine what master.page to use during the page load life-cycle.
So for the project I’m working on at the moment I really only need to brand the ‘file upload’ pages which are used by the users from the standard document library options menu. So, although this is unsupported by Microsoft’, I have little option but to modify the upload.aspx application page directly. Again you can find this in the layouts folder.
As long as you fully document this change and ensure your change control places check this during any future SharePoint software updates released from Microsoft you will be fine.
Look for the ‘PlaceHolderAdditionalPageHead’ content place holder then add the following code…
protected override voidOnPreInit(EventArgs e)
{
//Call the base class method first
base.OnPreInit(e);
//Dynamically change the MasterPageFile to the current sites
MasterPageFile = SPContext.Current.Web.CustomMasterUrl;
}
</script>

