Home > Sharepoint2010 > Modify Content Query webpart data programmatically

Modify Content Query webpart data programmatically

when ever we want to modify some data from content query webpart either we use out of the box XSLT functions or XSLT extensions.

but sometimes XSLT functions can not help us to achieve desired functionality and some times , some..times…XSLT extensions are not allowed, so what do to do in such scenario.

yes …YES..we have a way..we can modify the data of the content query webpart pro-grammatically.

lets see how..and how easy it is as compared to XSLT extensions.

I have a custom  list which has two columns

1) Title

2) LastName

and we have a need to combine Title with Title, lets see how this can be achieved by modifying content query webpart data programmatically.

List Data

Untitled

 [ToolboxItemAttribute(false)]
    public class ExtendedContentQueryDemoWebpart : ContentByQueryWebPart
    {

        protected override void OnInit(EventArgs e)
        {
            this.ProcessDataDelegate += new ProcessData(modifyData);
            base.OnInit(e);
        }

        private DataTable modifyData(DataTable dt)
        {
            foreach (DataRow item in dt.Rows)
            {
                item["Title"] = item["Title"].ToString() + " " + item["LastName"].ToString();
                item.AcceptChanges();
            }
            return dt;
        }

    }

Inherit your webpart class by ContentByQueryWebPart .

ContentByQueryWebPart class is present it Microsoft.SharePoint.Publishing.WebControls namespace which is present in Microsoft.SharePoint.Publishing.dll  which is located at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.Publishing.dll

this.ProcessDataDelegate : aha..this is the key, to this delegate we can attach a function which takes datatable as input and returns datatable as output.

Now I have deployed the webpart as a farm solution , also added it to the webpart page, basic configurations for content query webpart.

List from which we need to show the data

image

Fields to display [content query webpart, will query and bring these fields from the list.]

image

Click Ok..Aha we are done..so easy

it looks as below.

image

let me know if you have any questions.

Advertisements
Categories: Sharepoint2010
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: