Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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();
}
share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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