Archive

Archive for December, 2012

Modify Content Query webpart data programmatically

December 26, 2012 Leave a comment

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.

Categories: Sharepoint2010