SharePoint Sharpener

Obsessively Sharpening SharePoint

Archive for the ‘Development’ Category

Quick Fix: Remove Libraries, Lists From ToC

with 8 comments

 

I usually strive to first and foremost use SharePoint’s out of the box functionality to fulfil clients’ needs. Custom solutions with managed code, controlled deployment etc. are all well and good but sometimes you can get by with some simple frontend-configuration. This approach, if done right, enables clients to easier maintain and further enhance their SharePoint site once it’s up and running and the expensive SharePoint consultants have left the building.

Small adjustments can often be done with CSS or JavaScript/jQuery without remote desktop access to the server park. I know some hard core devs out there will oppose to making more or less unmanaged changes using something that hasn’t been pushed through a compiler. But like it or not, we need to get used to working within the constraints of Office 365 and SharePoint in the cloud. More about that in a later article.

Anyway, let’s skip the politics and get to the point of this post.

 

The problem

When you insert a standard Table of Contents web part it looks something like this:

image

And if you’re like most of my clients, you want it to look like this:

image

I.e. more “site mappy” without links to lists, document libraries and discussions.

 

The solution

This is the quick fix and therefore we’ll use client-side JavaScript to hide the unwanted nodes in the ToC.

First, create a library in the site to hold custom scripts. Why not call it “scripts”?

image

 

For easy editing, open the the new library in explorer view and create a text file, like so:

image

 

Open the text file in your favourite editor and paste the following JavaScript:

   1: <script language="JavaScript" type="text/javascript">

   2: // This script removes lists, libraries and discussions from Table of Contents

   3: // PeopleNet, tso@peoplenet.dk

   4: var links = document.body.getElementsByTagName("a");

   5: for(ii=0; ii<links.length; ii++)

   6: {

   7:     if(links[ii].outerHTML.indexOf("BaseType") != -1 && 

   8:       links[ii].parentNode.parentNode.parentNode.parentNode.className == "level-section")

   9:     {

  10:         links[ii].parentNode.parentNode.parentNode.parentNode.style.display = "none";

  11:     }

  12: }

  13: </script>

 

Save the file and go back to the SharePoint page where you placed the ToC web part. Below the ToC insert a Content Editor Web Part.

Back in the old days (i.e. SharePoint 2007) you’d edit JavaScript directly in the web part. This is still possible but it’s really quirky to work with. Instead, in the web part’s properties, point to the text file you created earlier:

image

 

This approach makes it so much easier to edit the JavaScript in a proper editor instead of in a SharePoint pop-up.

Click OK and save the page. The unwanted nodes are now hidden.

 

Note

I’m fully aware of the implications of the above approach with regards to solution management, client-side performance and so on, but every solution needs to be seen in the context of the need it fulfils. And sometimes the above approach hits the mark.

And remember, the Table of Contents web part is just one of site navigation options in SharePoint.

Written by Thomas Sondergaard

September 27, 2011 at 10:44 pm

Solution: Accessing SharePoint Site or WebApp via Code Returns Null

with 2 comments

A colleague of mine recently developed a timer job for SharePoint. The job worked fine on his developer and test machines but when he tried to activate it in the pre-production environment things went downhill.

An exception was thrown as soon as the timer job attempted to access the web application to add the timer job, ie. in this line of code:

SPContext.Current.Site.WebApplication.JobDefinitions.Add(timerJob)

The exception thrown was the good old “Object reference not set to an instance of an object” and further investigation showed that SPContext returned Null. Accessing the web application by referencing it directly (eg. SPWebApplication wA = SPWebApplication.Lookup(new Uri(http://site)); ) did not make a difference.

Coincidentally, the following error showed up in the event log: “Error in the site data web service”. This error mostly surfaces when SharePoint search chokes in a 32 bit environment due to large lists or inefficient use of memory capacity (heap fragmentation).

The solution

Restarting the below-mentioned services (net start/stop) fixed the problems for us:

net stop "Office SharePoint Server Search"
net start "Office SharePoint Server Search"
net stop "Windows SharePoint Services Search"
net start "Windows SharePoint Services Search"
net stop "Windows SharePoint Services Administration"
net start "Windows SharePoint Services Administration"
net stop "Windows SharePoint Services Timer"
net start "Windows SharePoint Services Timer"
iisreset /noforce

Source: Technet.

Written by Thomas Sondergaard

April 15, 2010 at 3:16 pm

SPC09: Overview of New Developer Features in SharePoint 2010

with 4 comments

 

At the SharePoint Conference in Las Vegas, Paul Andrew gave a thorough overview of the new features for developers in SharePoint 2010.

This is my modest attempt to convey Paul’s presentation in a simple blog post. Please note that the session contained a huge amount of information and I probably (definitely!) didn’t manage to take it all in. Read on at your own risk…

 

Development environment and tools

Traditionally, the path to your first SharePoint hello world-web part is littered with technical obstacles in the shape of tedious installation problems, Windows Server 2003 blues, virtual server nightmares etc. No more. Now you just need your laptop to develop SharePoint applications.

The box

SharePoint developers no longer need to run a Windows 2003 Server. In fact, SharePoint and the development tools can now be installed directly on Windows 7 or Vista (service pack 1). Either versions of SharePoint can be used, i.e. SharePoint Foundation (formerly WSS) or SharePoint Server.

The operating system must be 64 bit since SharePoint 2010 only runs in a 64 bit environment.

SharePoint on a desktop operating system is just for developers and not for running a production environment.

 

Visual Studio 2010

Many of SharePoint 2010’s new features can be accessed from inside Visual Studio 2010. Also, quite a few third-party add-ons may not be needed anymore as the functionality is now covered by Visual Studio.

Highlights:

  • Built-in designers for:
    • Web parts
    • BCS (formerly BDC)
    • Workflows
  • Package and deploy SharePoint projects
  • Generate WSPs
  • View SharePoint sites in server explorer
  • Integration to Team Foundation Server
  • Support for SharePoint sandboxed solutions
  • WSPs from SharePoint Designer can be imported (including workflows)
  • Build workflow steps for SharePoint Designer
  • SharePoint Business Connectivity Services support
  • New events projects templates

 

General improvents

Developer dashboard

A developer dashboard can be displayed automatically at the bottom of every page. The DD displays valuable information about how the page is generated, for instance:

  • Timing and duration of events
  • Database queries (even the ones SharePoint does in the background)

The DD is activated with the follow stsadm command:

stsadm –o setproperty –pn developer-dashboard –pv ondemand

 

Coding

Coding for SharePoint just got slightly easier due to the many improvements in .NET framework 4.0 and SharePoint 2010.

Below is a rundown of some of the new features:

  • LINQ for SharePoint
  • After-synchronous events
  • New event types:
    • Site-scoped events
    • Web creation events
  • Workflow improvements:
    • Initiation and association forms in Visual Studio
    • New design user interface for workflows in SharePoint Designer
    • Use Visio 2010 to design workflows
  • SharePoint UI can now be saved as a template
  • WSP is now the unified developer format – works in site collections and machine

 

Improvements to lists

In SharePoint 2007, lists can cause any developer headaches. Hopefully, the new list architecture in SharePoint 2010 will solve this. Below is an overview of some of the improvements:

  • Validation with Excel-like Formula – forms can be validated using simple syntax
  • Lookup to multiple columns
  • Lookup fields have true relations which ensure proper deletion (transaction-style)
  • List index auto-creation
  • Scalability and performance vastly improved:
    • Lists and folders can now contain a million elements
    • Document libraries can contain 10 million documents
  • List query throttling
  • Lists views no longer based on CAML but XSL-T. Queries still use CAML

 

Ribbon and Dialog framework

The ribbon we know (and some love) from Microsoft Office is now used in SharePoint 2010.

Some of the highlights:

  • Custom actions can be embedded in the ribbon
  • The ribbon is context sensitive
  • The SharePoint out-of-the-box forms are replaceable
  • New web dialog functionality:
    • A dialog floats on top of the SharePoint page and is used to get input from the user
    • Fully programmable

 

Silverlight 3

Silverlight plays an important part in SharePoint 2010 and is used in many of the improved UI elements. From a developer’s point of view, these are the highlights:

  • Built-it and customisable media player web part
  • List and site creation from within Silverlight
  • Office web applications run in Silverlight
  • Client object model – call SharePoint APIs from within Silverlight

Written by Thomas Sondergaard

October 19, 2009 at 9:48 pm

Crawl Problem with Multiple Value Lookup Fields Acknowledged by Microsoft

leave a comment »

Last year we at PeopleNet ran into a problem when using lists with many columns, i.e. around 1600!

Indexing such a list would almost always fail with a timeout or out of memory error in the log, even though SharePoint is supposed to be able to handle at least 2000 columns without performance issues.

We corresponded back and forth with a Microsoft support engineer about the problem and it turned out that lists with many multiple value lookup fields will bring the SQL Server to its knees fairly quickly.

Microsoft has recently released a KB article in relation to this, however, it doesn’t specifically single out multiple value lookup fields as the culprit, although they almost always are.

Written by Thomas Sondergaard

August 13, 2009 at 7:39 am

How To Always Link to the Right Application Pages

with one comment

As a SharePoint developer you’ve probably run into this problem several times: How to make sure that links to application pages stay valid irrespective of where within a site collection your web part is placed.

If you hand-code links you’re likely to get the path relative to the root site wrong. Even worse, if the the filename of an application page is changed, e.g. from DispForm.aspx to DispFormNew.aspx, your links will surely break.

 

The application pages

Generally, a SharePoint list contains the following application pages:

  • AllItems.aspx
    Shows all items in the list
  • DispForm.aspx
    Displays a read-only version of a list item
  • NewForm.aspx
    Form for creating a new list item
  • EditForm.aspx
    Form for editing an existing list item

More applications pages exist but these are the ones most commonly used.

 

Ensuring unbroken links programmatically

The PAGETYPE Enumeration gives you access to the application pages and enables you to build links that always work.

Assuming you have already created your SPList and SPListItem objects the below snippet builds a link to the correct DispForm:

list.Forms[PAGETYPE.PAGE_DISPLAYFORM].ServerRelativeUrl.ToString();

Of course, the same applies to NewForm and EditForm.

 

Linking to the default view of a list

Below a list of elements you may wish to link to the underlying list’s default view. In many cases this will be the AllItems.aspx page but what happens if a user changes the default view in the settings of the list? If you’ve hardcoded a link to AllItems.aspx you may be lucky that the link still works but if AllItems.aspx has been renamed or removed, you’re out of luck.

Instead of hardcoding the link you should use PAGE_DEFAULTVIEW, like this:

list.Forms[PAGETYPE.PAGE_DEFAULTVIEW].ServerRelativeUrl.ToString();

Simple, isn’t it?

Written by Thomas Sondergaard

May 16, 2009 at 3:01 pm

Programmatically Checking if a SharePoint Element is Published

with 3 comments

Here’s a quick tip for you: How to check if the latest version of an element in a SharePoint list is published.

Once you’ve got hold of an SPListItem, you can check the Versions property (an array containing all the versions of the element).

In this block of code IsPublished becomes True if the latest version is published:

bool IsPublished = TheItem.Versions[0].Level == SPFileLevel.Published ? true : false;

 

That’s it.

Written by Thomas Sondergaard

March 26, 2009 at 6:25 pm

JavaScript Tips for SharePoint

with one comment

I’ve gatherered a few productive tips for using JavaScript within SharePoint. The below commands can be used within webparts etc. or directly in the browser’s address field while you’re on a SharePoint page, like this:

Putting javascript: in front of your script snippet will fire it right there and then.

These commands are neither terribly advanced nor applicable on all pages, but they make for a useful reference all the same. I aim to continually update this page with new content so if you have any JavaScript tips, feel free to drop them in the comments.

 

Edit an application page: MSOLayout_ToggleLayoutMode()

This enables edit mode on SharePoint’s application pages, like EditForm.aspx, DispForm.aspx etc. so you can insert web parts above or below the normal content:

 

Pop-up an alert with the User ID og the logged-in user: alert(_spUserId)

 

 

Enable the Edit toolbar: javascript:__doPostBack(‘ctl00$PlaceHolderTopNavBar$SiteActionsMenuMain$ctl00$wsaShowMenu_CmsActionControl’,’reviewPage’)

Simply enables the toolbar where you can access the page edit functions:

 

 

Hide the Edit toolbar: __doPostBack(‘ctl00$PlaceHolderTopNavBar$SiteActionsMenuMain$ctl00$wsaHideMenu_CmsActionControl’,’exitMenu’)

No prizes for guessing this one.

 

Show help: TopHelpButtonClick(‘NavBarHelpHome’)

Pops up the built-in help page. Interestingly, you can try this on Microsoft’s SharePoint Conference web site:

Written by Thomas Sondergaard

March 23, 2009 at 11:31 pm

Posted in Development

Tagged with , ,