Saturday, August 23, 2008

Issues with Data View webpart programmatically

First of All Special Thanks to Jason Huh for his article
Creating a DataView webpart programmatically



Data view web part is useful in SharePoint for filtering data from particular list according to business logic.

We had successfully created data form view (data view) webpart programmatically with the reference of above site.
But we are facing some problems
1) The XslText property is empty (Major one)
2) Sorting is not working
3) Title Link dropdown is not coming (ECB menu is not coming).
4) Getting JavaScript error on mouseover on title link. (ctx… invalid object)
5) Filtering is not working. ( still not solved and working on that )

Now to solve this problem we found that we need to set some properties for Data view webpart.

The XslText property is empty
Before you start coding you need to understand that data view webpart needs xsl or xsl link property to be set for rendering.

And you have to take xsl from Sharepoint Designer.
We are hoping you must how to take XSL from SharePoint designer.

This error comes when proper xml is not supplied to data view webpart.
The easy way to resolve this is Create XSL file.
Copy all the Content between the tags <XSL> and </XSL> in that file.

Then upload that XSL file in “Style Library/Xsl Style Sheet” Folder. If you do not have style library folder in you document library then activate “Office SharePoint Server Publishing Infrastructure” feature from Site collection feature

In coding give full path of the

dataFormWebPart.XslLink =
"http://sharepointkings/sites/dataviewtestsite/Style Library/XSL Style Sheets/testing.xsl";

If your XSL is correct then surly your XslText is empty error will solved.

Now moving forward to other errors.

Most of all the errors will be solved by setting all the property which had been set by SharePoint designer when we create webpart from it.

Here we go for code.

Please read comment in the code to understand code better.
The code has been manipulated so that someone can understand properly.


using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Globalization;




namespace Sharepointkings.CustomWebPart
{
[
DefaultProperty("Text"),
ToolboxData("<{0}:MyDataFormWebPart runat=server></{0}:MyDataFormWebPart>"),
XmlRoot(Namespace = "Sharepointkings.CustomWebPart")]
public class MyDataFormWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
DataFormWebPart dataFormWebPart;
protected override void CreateChildControls()
{

SPSite siteColl = SPContext.Current.Site;
SPWeb site = SPContext.Current.Web;

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID))
{
using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(site.ID))
{
dataFormWebPart =
new DataFormWebPart();
dataFormWebPart.Title = " testing ";
dataFormWebPart.SuppressWebPartChrome = false;
dataFormWebPart.FrameType = FrameType.Default;
dataFormWebPart.DetailLink = “/sites/dataviewtestsite/Lists/testingdata /AllItems.aspx";
dataFormWebPart.ExportControlledProperties = true;
dataFormWebPart.IsVisible = true;
dataFormWebPart.AllowRemove = true;
dataFormWebPart.AllowEdit = true;
dataFormWebPart.FrameState = FrameState.Normal;
dataFormWebPart.AllowConnect = true;
dataFormWebPart.IsIncludedFilter = "";
dataFormWebPart.TitleUrl = " /sites/dataviewtestsite /Lists/testingdata/AllItems.aspx";
dataFormWebPart.ShowWithSampleData = false;
dataFormWebPart.HelpMode = WebPartHelpMode.Modeless;
dataFormWebPart.ExportMode = WebPartExportMode.All;
dataFormWebPart.ViewFlag = "9"; // we don’t why this 9 is for but in SPD it set as 9 you can set accordingly

dataFormWebPart.HelpLink = "";
dataFormWebPart.AllowHide = true;
dataFormWebPart.AllowZoneChange = true;
dataFormWebPart.PartOrder = 1;
dataFormWebPart.UseSQLDataSourcePaging = true;
dataFormWebPart.IsIncluded = true;
dataFormWebPart.NoDefaultStyle = "TRUE";

dataFormWebPart.XslLink =
"http://sharepointkings/sites/dataviewtestsite/Style Library/XSL Style Sheets/testing.xsl";

dataFormWebPart.ParameterBindings = parabindings();
SPList list = ElevatedSite.Lists["testingdata "];
dataFormWebPart.ListName = list.ID.ToString();
string query = string.Empty;
query = @"<Query><Where><Gt><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Gt></Where></Query>";
uint maximumRow = 100;
SPDataSource dataSource = GetDataSource("MyWebPartdataSource", ElevatedSite.Url, list, query, maximumRow);
dataFormWebPart.DataSources.Add(dataSource);
this.Controls.Add(dataFormWebPart);
}
}
});

}

private string parabindings()
{
// now this section you have to replace with your parabindings
// you will get this values from SPD just before <XSL> tag
StringBuilder strb = new StringBuilder();
strb.Append(“”);
//strb.Append("<ParameterBindings>");
// strb.Append("<ParameterBinding Name=\"PageUrl\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"PagePath\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"HttpHost\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"HttpPath\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"List\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"URL_Display\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"HttpVDir\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"View\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"FilterLink\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"Language\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"dvt_adhocmode\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"dvt_fieldsort\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"dvt_sortfield\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"dvt_sortdir\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"dvt_filterfield\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"dvt_firstrow\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"dvt_p2plinkfields\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"dvt_nextpagedata\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"dvt_sorttype\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"dvt_apos\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"filterParam\" Location=\"Postback;Connection\"/>");
// strb.Append("<ParameterBinding Name=\"ImagesPath\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"ListUrlDir\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"EMail\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"Userid\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"URL_DISPLAY\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"URL_EDIT\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"WebQueryInfo\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"URL_Edit\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"URL_Lookup\" Location=\"Postback;Connection;CAMLVariable\"/>");
// strb.Append("<ParameterBinding Name=\"UserID\" Location=\"CAMLVariable\" DefaultValue=\"CurrentUserName\"/>");
// strb.Append("<ParameterBinding Name=\"Today\" Location=\"CAMLVariable\" DefaultValue=\"CurrentDate\"/>");
// strb.Append("<ParameterBinding Name=\"dvt_filterfields\" Location=\"Postback;Connection\"/>");
// strb.Append("<ParameterBinding Name=\"dvt_partguid\" Location=\"Postback;Connection\"/>");
// strb.Append("<ParameterBinding Name=\"dvt_groupfield\" Location=\"Postback;Connection\"/>");
// strb.Append("<ParameterBinding Name=\"dvt_startposition\" Location=\"Postback\" DefaultValue=\"\"/>");
// strb.Append("</ParameterBindings>");
return strb.ToString();
}


SPDataSource GetDataSource(string dataSourceId, string webUrl, SPList list, string query, uint maximumRow)
{
SPDataSource dataSource = new SPDataSource();
dataSource.UseInternalName =
true;
dataSource.ID = dataSourceId;
dataSource.DataSourceMode =
SPDataSourceMode.List;
dataSource.List = list;
dataSource.UseInternalName = true;
dataSource.IncludeHidden = true;
dataSource.SelectCommand =
"<View>" + query + "</View>";
Parameter listIdParam = new Parameter("ListID");
listIdParam.DefaultValue = list.ID.ToString(
"B").ToUpper();

Parameter maximumRowsParam = new Parameter("MaximumRows");
maximumRowsParam.DefaultValue = maximumRow.ToString();

Parameter StartRowIndex = new Parameter("StartRowIndex");
StartRowIndex.DefaultValue = "0";

Parameter nextpagedata = new Parameter("nextpagedata");
nextpagedata.DefaultValue = "0";

QueryStringParameter rootFolderParam = new QueryStringParameter("RootFolder", "RootFolder");

dataSource.SelectParameters.Add(listIdParam);
dataSource.SelectParameters.Add(rootFolderParam);
dataSource.SelectParameters.Add(StartRowIndex);
dataSource.SelectParameters.Add(nextpagedata);
dataSource.SelectParameters.Add(maximumRowsParam);

dataSource.UpdateParameters.Add(listIdParam);
dataSource.DeleteParameters.Add(listIdParam);
dataSource.InsertParameters.Add(listIdParam);
return dataSource;
}

}
}


filtering is not working and Still we are working for the same.

2 comments:

Unknown said...

Hi not sure if your still chacking comments on this post, but you mention some issues that decribe exactly what im facing. i have a data view web part and im trying to create a tooltip on hover, for some reason when i use onmouseover i get the invalidobject problem, it does see the code but it rejects it. thinking that it might be a javascript/SPS thing i used straight css tooltip unhiding when mouse over in this case the tooltip would show but with no data as if not allowing the {@title} to be access. do you know of a standard way to create a mouseover tooltip for sharepoint xsl or could you offer some advice o what settings i could check to see it any helps. thanks again

Anonymous said...

hi!, thanks for the article!

you can find a fix for the column head filtering here

http://jamestsai.net/Blog/post/How-to-query-cross-site-lists-in-DataFormWebPart-Part-3-Filtering-on-column-headers-problem.aspx




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com