Nice one Ishai - thanks for that, and as always raising that bar!!!

Well done
________________________________________
From: [EMAIL PROTECTED] [EMAIL PROTECTED] On Behalf Of Ishai Sagi [EMAIL 
PROTECTED]
Sent: Thursday, 13 December 2007 8:45 AM
To: [email protected]
Subject: RE: [OzMOSS] Override Search Result Webpart

Here is a sample of what I am going to post.
This example creates a web part that supports XSLT, and does a query on lists 
of type "Documents" (101) to return all items that have the word "test" in the 
title.

I have also attached the whole sample as a visual studio solution. note however 
that I am using the visual studio extentions 1.1, and without which you will 
not be able to open the project (but you can still open the class files).

This is ofcourse just a sample - not production code. If I had more time I 
would add a toolpart that would allow you to choose types of libraries, define 
the query and so on. As it is, everything is hard coded.

oh, one more thing - since I didnt include a built-in xslt, the web part will 
tell you that the xslt is empty. i have attached a sample xslt that displays 
the titles in a table.

Code:


using System;

using System.Runtime.InteropServices;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Serialization;

using Microsoft.SharePoint;

using Microsoft.SharePoint.WebControls;

using Microsoft.SharePoint.WebPartPages;

using System.Data;

using System.Xml;

namespace AdvancedQueryWebPart

{

[Guid("46a8853d-415c-458e-990c-419c12fa04f5")]

public class AdvancedQueryWebPart : DataFormWebPart, IWebPart

{

public AdvancedQueryWebPart()

{

this.ExportMode = WebPartExportMode.All;

}

protected override void SetDataSourceProperties()

{

try

{

// Call a custom function that returns the data you want to show as a data table

DataTable results = GetCustomData();

if (results.Rows.Count > 0)

{

// generate xml for selected items

XmlDocument doc = new XmlDocument();

XmlNode root = doc.AppendChild(doc.CreateElement("Rows"));

foreach (DataRow row in results.Rows)

{

XmlElement rowNode = doc.CreateElement("row");

foreach (DataColumn col in row.Table.Columns)

{

string val = row[col].ToString();

XmlAttribute att = doc.CreateAttribute(col.ColumnName);

att.Value = val;

rowNode.Attributes.Append(att);

}

root.AppendChild(rowNode);

}

// create an XmlDatasource with the new data, and set it to cache for one second

XmlDataSource ds = new XmlDataSource();

ds.CacheDuration = 1;

ds.Data = doc.InnerXml;

// bind the web part to the xml

this.DataSource = ds;

this.DataBind(true);

}

else

{

Label noResults = new Label();

noResults.Text = "No results were found";

this.Controls.Add(noResults);

}

}

catch (Exception ex)

{

Label lblError = new Label();

lblError.Text = ex.ToString();

this.Controls.Add(lblError);

}

base.SetDataSourceProperties();

}

private DataTable GetCustomData()

{

try

{

SPWeb webSite = SPContext.Current.Web;

SPSiteDataQuery query = new SPSiteDataQuery();

//look only in document libraries

query.Lists = "<Lists ServerTemplate=\"101\" />";

//search for documents that have "Test" in the title

query.Query = @"<Where><Contains><FieldRef Name=""Title"" /><Value 
Type=""Text"">Test</Value></Contains></Where>";

//bring back the title field of the documents

query.ViewFields = @"<FieldRef Name=""Title"" Nullable=""TRUE"" /><FieldRef 
Name=""FileLeafRef"" Nullable=""FALSE"" />";

DataTable items = webSite.GetSiteData(query);

return items;

}

catch (Exception ex)

{

throw new Exception("There was a problem querying the site with the query", ex);

}

}

}

}



Ishai Sagi
Solution Architect
Information Management
MVP Microsoft Office SharePoint Server

Direct:    02 8001 7717
Fax:       02 8001 7778
Mobile:   0423 791 728
Email:    [EMAIL PROTECTED]
Web:     www.uniqueworld.net<http://www.uniqueworld.net/>
Blog:         www.sharepoint-tips.com<http://www.sharepoint-tips.com/>
innovative business solutions that make a difference


________________________________
From: [EMAIL PROTECTED] [EMAIL PROTECTED] On Behalf Of Ishai Sagi [EMAIL 
PROTECTED]
Sent: Thursday, 13 December 2007 7:41 AM
To: [email protected]
Subject: RE: [OzMOSS] Override Search Result Webpart

Mick,
Using the DataFormWebPart as a base class, and overriding its xml data source 
means that there are no listids or webids.

infact - what you are thinking of is datasources that were defined by 
sharepoint designer.
See, what sharepoint designer does when you add a dataform, is create an xml 
datasource in the page (with listids and webids) and then add a dataformwebpart 
and bind it to the xml data source.

By us using the webpart and binding it to our own datasource, we have no issues 
of deployment.

again - I will post an example.

________________________________
From: [EMAIL PROTECTED] [EMAIL PROTECTED] On Behalf Of Mick Badran [EMAIL 
PROTECTED]
Sent: Wednesday, 12 December 2007 2:48 PM
To: [email protected]
Subject: RE: [OzMOSS] Override Search Result Webpart

Ishai – just be careful that the dataform webpart doesn’t deploy well.

Has various listIDs and WebIDs in its XML config.

Cheers,

Mick.

From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Kanwartej Singh 
Basrai
Sent: Wednesday, 12 December 2007 2:44 PM
To: [email protected]
Subject: Re: [OzMOSS] Override Search Result Webpart

Thanks mate I will look into that. Do you by any chance have any code examples 
to get me started?

cheers
Tej
On Dec 12, 2007 2:30 PM, Ishai Sagi <[EMAIL PROTECTED]<mailto:[EMAIL 
PROTECTED]>> wrote:

My advice would be to override the DataFormWebPart which gives you the XSLT 
options, and then override SetDataSourceProperties and set a new datasource to 
the webpart.

I am planning a blog post about that, but it may take a while.



From: [EMAIL PROTECTED]<mailto:[EMAIL PROTECTED]> [mailto: [EMAIL 
PROTECTED]<mailto:[EMAIL PROTECTED]>] On Behalf Of Kanwartej Singh Basrai
Sent: Wednesday, 12 December 2007 2:28 PM
To: [email protected]<mailto:[email protected]>
Subject: [OzMOSS] Override Search Result Webpart



Hi Guys



Has anyone tried to override the search results webpart with a custom one? I am 
trying to create a custom search results webpart that allows for wild card 
searches. I managed to get the wildcard searches work by creating a webpart 
which inherits from the  Microsoft.SharePoint.Portal.WebControls.SearchResults 
and overriding the oninit method, but I am having troubles with rendering the 
control. Ideally i would like to create a custom property that can take in an 
XSLT and transform the search results based on that like the core search result 
webpart works, but any help in rendering the results would be helpful.



Cheers

Tej

------------------------------------------------------------------- OzMOSS.com 
- to unsubscribe from this list, send a message back to the list with 
'unsubscribe' as the subject.
Powered by mailenable.com<http://mailenable.com/> - List managed by 
www.readify.net<http://www.readify.net/>

No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.503 / Virus Database: 269.16.17/1179 - Release Date: 9/12/2007 
11:06 AM
No virus found in this outgoing message. Checked by AVG Free Edition. Version: 
7.5.503 / Virus Database: 269.16.17/1179 - Release Date: 9/12/2007 11:06 AM
------------------------------------------------------------------- OzMOSS.com 
- to unsubscribe from this list, send a message back to the list with 
'unsubscribe' as the subject.
Powered by mailenable.com<http://mailenable.com/> - List managed by 
www.readify.net<http://www.readify.net/>

------------------------------------------------------------------- OzMOSS.com 
- to unsubscribe from this list, send a message back to the list with 
'unsubscribe' as the subject.
Powered by mailenable.com - List managed by www.readify.net
------------------------------------------------------------------- OzMOSS.com 
- to unsubscribe from this list, send a message back to the list with 
'unsubscribe' as the subject.
Powered by mailenable.com - List managed by www.readify.net
------------------------------------------------------------------- OzMOSS.com 
- to unsubscribe from this list, send a message back to the list with 
'unsubscribe' as the subject.
Powered by mailenable.com - List managed by www.readify.net
------------------------------------------------------------------- OzMOSS.com 
- to unsubscribe from this list, send a message back to the list with 
'unsubscribe' as the subject.
Powered by mailenable.com - List managed by www.readify.net


------------------------------------------------------------------- OzMOSS.com 
- to unsubscribe from this list, send a message back to the list with 
'unsubscribe' as the subject.
Powered by mailenable.com - List managed by www.readify.net


Reply via email to