SharePoint document library list view webpart drag and drop

The following article assumes that you know how to add programmatically list view web parts on your page.

You might find yourself being in a situation in which you are programmatically adding a List View Webpart pointing to a document library on your site and you wonder why:

  • ‘add document’ link does not show
  • drag & drop does not show up or does not work

Basically, you have ended up with something along the lines of:

drag drop not working

First, and foremost, you have to change your code and instead of adding ListViewWebPart, you have to add XsltListViewWebPart (source).

var webPartToAdd = new XsltListViewWebPart
                        {
                            ChromeState = PartChromeState.Normal,
                            ChromeType = PartChromeType.None,
                            Title = "Documents List View",
                            ListName = web.Lists["Documents"].ID.ToString("B").ToUpper(),
                            XmlDefinition = web.Lists["Documents"].DefaultView.GetViewXml()
                        }

This will get you to the point where you see the drag&drop functionality (in IE only, I don’t see it in Firefox):

drop here

However, when you drop the file, you might get the error:

exception

The reason for this error is the fact that sp.js and sp.core.js are not registered on the page (SP.Utilities and SP.Utilities.CommandBlock are undefined)! I think this is a bug in SharePoint, because if you simply click Edit on the page and then save it (without changing anything), the web part will start working (because the scripts will now be registered on the page).

So, how do we overcome this issue? Well, I have determined that if we execute the following script on the page, the drag&drop in the web part will work fine:

SP.SOD.executeFunc('sp.js', 'SP.Utilities.Utility', function() { console.log('sp.js loaded'); });
SP.SOD.executeFunc('sp.core.js', 'SP.Utilities.CommandBlock', function() { console.log('sp.core.js loaded'); });

This code will include the scripts that SharePoint ‘forgets’ to include. You have variety of options how to register those scripts on the page. I have decided to include them as a content of a Content Editor WebPart which I provision on the same page where I provision the (xslt) list view web part:

// add script registration work-around (there is a bug in the framework, which does not register those scripts until we edit the page).
var xmlDoc = new XmlDocument();
var xmlElement = xmlDoc.CreateElement(“HtmlContent”);
xmlElement.InnerText = “<script type=’text/javascript’>SP.SOD.executeFunc(‘sp.js’, ‘SP.Utilities.Utility’, function() { console.log(‘sp.js loaded’); }); SP.SOD.executeFunc(‘sp.core.js’, ‘SP.Utilities.CommandBlock’, function() { console.log(‘sp.core.js loaded’); })</script>”;
var cewp = new ContentEditorWebPart
{
ChromeState = PartChromeState.Normal,
ChromeType = PartChromeType.None,
Title = “Drag and drop script registration workaround”,
Content = xmlElement
};

And then, viola!, the drag&drop works:

viola

 

I hope this helps,

Hristo

ChartPart for SharePoint

If you would like to generate some charts in Microsoft SharePoint 2007, after a quick search you will find that there is no such out-of-the-box functionality. You shouldn’t worry, though, as there is a free web part available at Codeplex, which has everything you need for your list-based charting needs. It is called “ChartPart for SharePoint” and is available here.

Prerequisites

In order to get it working on your server, you will need to have Microsoft .NET 3.5 SP1 and Microsoft Chart Controls for Microsoft .NET Framework 3.5 installed. The ‘installation package’ does not check for those prerequisites, so make sure you do that manually before you install. Most probably you already have .NET 3.5 SP1 installed, but chances are, the MS Chart Controls are not installed.

Installation

The deployment package consists of two WSP files (SharePoint solution deployment packages) – MSChartControls.wsp and ChartPart 2.0.wsp. First, you have to install the MSChartControls package. Login on your SharePoint server, start the Console (Start -> Run -> cmd) and type:

stsadm -o addsolution –filename MSChartControls.wsp
stsadm -o execadmsvcjobs

STSADM NOTE: Of course, you will have to choose between the convenience of not typing the full path to the stsadm.exe file or not having to type the full path to the WSP. So, you have to be in one of the two folders and type the full path to the other file.

This will add the solution to the SharePoint Central Administration -> Operations -> Solution Management. You can either use the User Interface it provides to deploy the added solution, or you can continue executing console commands, like this:

stsadm -o deploysolution -name MSChartControls.wsp -immediate –allowgacdeployment
stsadm -o execadmsvcjobs

Then, you have to activate the installed solution on Web Application level:

  1. Go to Central Administration
  2. Go to Application Management
  3. Select Manage Web Application Features
  4. Verify that you have selected the correct Web Application in the top right corner
  5. Click Activate on the Microsoft Chart Controls Feature

Now, you have to do something similar for the other WSP (Chart Part 2.0.wsp):

stsadm -o addsolution -filename “ChartPart 2.0.wsp”
stsadm -o execadmsvcjobs

Deploy the solution, either from Central Admin, or by using the console:

stsadm -o deploysolution -name “Chartpart 2.0.wsp” -immediate -allowCasPolicies -url <url>
stsadm -o execadmsvcjobs

Where <url> is the address of the web site you wish to deploy the webpart to. Now all you have to do is activate the feature on the selected Site Collection.

  1. Go to Site Settings for your top-level site
  2. Go To Site Collection Features
  3. Activate the ChartPart for SharePoint feature
  4. Now you can add ChartParts on every site in your Site Collection.

Usage

I created a new ‘Web Part Page’ on my site, to use for experimenting with the web parts. I chose ‘Full Page, Vertical’ layout template, as it  is one of the simplest and I preferred to have full width.

I chose to ‘Edit Page’ and clicked on ‘Add a Web Part’ link in the design mode of the page. A pop-up showing all available web parts appears and from there, I chose to add on the page the ‘ChartPart’ web part.


Once added on the page, you have to configure it. While the page is still in design mode, click on the little arrow down in the right upper corner of the web part. It will open a side menu. Click on ‘Modify Shared Web Part’. This will open the options of the web part.

Here, you can set the title of the web part (as it will appear on the page it is added to), the web site to pull data from, the specific list and its view. Then you have to select X and Y “Series” column. A bit of explanation… Only Number fields can be choses as “X-Series”. “Item Count” is always available to you. As “Y-Series” you have to specify a column of the SharePoint list.

To illustrate all this, I will configure my list to contain only two columns – “City” (single line of text) and “Population” (number field). Here is what my list looks like:

Now, I want to configure the Chart web part to visualize this statistics. I will set it up this way:

We click OK button and the generated chart looks like:

Customization

You can customize the chart in many ways. First. by selecting the ‘Chart Type’ for the chart. Choices are Column, Bar, Pie and so many others. Additionally, you can select ‘Style’ (Cylinder, Emboss, LightToDark, Wedge), which seems to work only on specific ‘Chart Types’. For example, it clearly works on a ‘Column’ chart type. Here is the difference between ‘Cylinder’ and ‘Wedge’:

There is many other fine tunings you can do, like color palette, size and etc. Also what’s really nice is that in the tooltip (when you hover a column in the chart) you will see an exact value, of the column.

Conclusion

ChartPart for SharePoint is a great component, if you want to visualize some statistics based on SharePoint list items. It is highly customizable, easy to install and use.