0

I am customizing xsltlist view webpart using c#. I am able to get the results properly till I give the view name to the code. As soon as I give it it's not rendering the data. It's only displaying the grouping in the view. Below is my code for the same. Can anyone let me know what is wrong in this. Here is the code:

[WebBrowsable(true),
    Personalizable(PersonalizationScope.Shared),
    Category("Custom Properties"),
    WebDisplayName("Web Url"),
    WebDescription("Enter the Web Url")]
    public string WebUrl { get; set; }

    [WebBrowsable(true),
    Personalizable(PersonalizationScope.Shared),
    Category("Custom Properties"),
    WebDisplayName("List Name"),
    WebDescription("Enter the List Name")]
    public string ListName { get; set; }

    [WebBrowsable(true),
    Personalizable(PersonalizationScope.Shared),
    Category("Custom Properties"),
    WebDisplayName("View Name"),
    WebDescription("Enter the View Name")]
    public string ViewName { get; set; }

if (!string.IsNullOrEmpty(ListName))
                        {
                        SPList list = web.Lists[ListName.ToString()];

                            XsltListViewWebPart xsltwp = new XsltListViewWebPart();
                            xsltwp.ListName = list.ID.ToString("B").ToUpperInvariant();
                            xsltwp.TitleUrl = list.DefaultViewUrl;
                            xsltwp.WebId = list.ParentWeb.ID;
                            xsltwp.Title = list.Title;
                            xsltwp.ListUrl = list.DefaultViewUrl;
                            xsltwp.ListId = list.ID;
                            xsltwp.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;

                            if (!string.IsNullOrEmpty(ViewName))
                            {
                                SPView defaultView = list.Views[ViewName];
                                xsltwp.ViewId = Int32.Parse(defaultView.BaseViewID);
                                xsltwp.ViewGuid = defaultView.ID.ToString("B").ToUpper();
                            }

                            //xsltwp.XmlDefinition = view.GetViewXml();

                            this.Controls.Add(xsltwp);

                        this.Controls.Add(xsltwp);
                    }

Thanks in advance.

0

Are you sure the view exists. Try putting following code in try catch block

if (!string.IsNullOrEmpty(ViewName))
{
    SPView defaultView = list.Views[ViewName];
    xsltwp.ViewId = Int32.Parse(defaultView.BaseViewID);
    xsltwp.ViewGuid = defaultView.ID.ToString("B").ToUpper();
}
  • Hi Nadeem, Thanks for your input but I was facing the problem with XsltListViewWebPart control. It was not allowing me to display the data. Instead of using XsltListViewWebPart I have used ListViewWebPart and it worked. :) – Ankit Sep 10 '13 at 4:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.