2

I'm adding an ListViewWebPart programmatically to my custom page, so far so good, the problem is that the link "Add document" is missing(Summary Toolbar), and the simple behavior of open the edit web part panel, don't change a thing, and then press OK resolves the prob. (and also adds Ajax Options and Misc categories)

Any idea why is missing or how to force it to appear?

spregards

code:

public static void AddListViewWebPart(SPWeb spWeb, SPFile oFile, SPList oSPList, string webPartTitle, string viewName, string webPartZone)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    using (var site = new SPSite(spWeb.Site.ID))
                    {
                        site.AllowUnsafeUpdates = true;
                        using (SPWeb oWeb = site.OpenWeb(spWeb.ID))
                        {
                            oWeb.AllowUnsafeUpdates = true;
                            //Create the object SPLimitedWebPart Manager
                            SPLimitedWebPartManager limitedWebPartManager = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared);

                            //Create ListViewWebPart
                            SPView spView = oSPList.Views[viewName];
                            var oListViewWP = new ListViewWebPart
                                              {
                                                  ChromeType = PartChromeType.None,
                                                  Title = webPartTitle,
                                                  ListName = oSPList.ID.ToString("B").ToUpper(),
                                                  ViewGuid = spView.ID.ToString("B").ToUpperInvariant(),
                                                  AllowConnect = false,
                                                  AllowClose = false,
                                                  AllowEdit = false,
                                                  AllowHide = false,
                                                  AllowMinimize = false,
                                                  AllowZoneChange = false,
                                                  ListViewXml = spView.HtmlSchemaXml,
                                              };

                            //Define the zone in which webparts need to be added
                            limitedWebPartManager.AddWebPart(oListViewWP, webPartZone, 1);
                            SetToolbarTypeFreeForm(oWeb, oFile.Url, oListViewWP.StorageKey);
                            limitedWebPartManager.SaveChanges(oListViewWP);
                            limitedWebPartManager.Dispose();
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("WebPartsHelper.AddListViewWebPart() Error", TraceSeverity.Medium, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
                SPUtility.TransferToErrorPage(String.Format("An error occurred while attempting to add the List View WebPart to the default.aspx with the following error message: {0}", ex.Message));
            }
        }

Solution:

using (SPLimitedWebPartManager wpMngr = web.GetLimitedWebPartManager("default.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
                {
                    //// Create web part and set properties    
                    XsltListViewWebPart lvwp = new XsltListViewWebPart
                                               {
                                                   ListName = oList.ID.ToString("B").ToUpper(),
                                                   Title = oList.Title,
                                                   ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.TitleOnly,
                                                   ViewGuid = spView.ID.ToString(),
                                                   ListId = oList.ID,
                                                   XmlDefinition = spView.GetViewXml()

                                               };
                    wpMngr.AddWebPart(lvwp, "Left", 1);
                }
2
  • Can you post the code you're using to add the web part? Commented Jul 4, 2012 at 14:43
  • This will get you the 'drop here' to show, but when you actually drop a file, you will get an error about SP.Utilities.CommandBlock being undefined. No? Someone else has noticed the same thing also here - responsivesharepoint.codeplex.com/discussions/434611
    – hyankov
    Commented Feb 25, 2014 at 11:40

2 Answers 2

2

fallout,

I had the same problem and it was solved simply by using XsltListViewWebPart class instead ListViewWebPart.

Hope that it helps,

1
  • Obrigado Daniel!
    – fallout
    Commented Oct 3, 2012 at 14:49
0

not fancy (at all!!) workaround solution

right after the listviewwebpart zone:

//workaround to show 'new document' link 
var contentEditor = new ContentEditorWebPart(); 
string content = String.Format("<table><tbody><tr>" + 
                                "<td class='ms-addnew' style='padding-bottom: 3px'>" + 
                                "<span class='s4-clust' style='position: relative; width: 10px; display: inline-block; height: 10px; overflow: hidden'>" + 
                                "<img src='/_layouts/images/fgimg.png' alt='' style='position: absolute; top: -128px !important; left: 0px !important'/>&#160;</span>&#160;" + 
                                "<a onclick='javascript:NewItem2(event, &quot;{0}/_layouts/Upload.aspx?List={1}&amp;RootFolder=&quot;);javascript:return false;'" + 
                                "href='{2}/_layouts/Upload.aspx?List={1}&amp;RootFolder='; target='_self'>Add document</a>" + 
                                "</td></tr></tbody></table>", oWeb.Url, "{" + oSPList.ID + "}", oWeb.ServerRelativeUrl); 
var xmlDoc = new XmlDocument(); 
XmlElement xmlElement = xmlDoc.CreateElement("Root"); 
xmlElement.InnerText = content; 
contentEditor.Content = xmlElement; 
contentEditor.Content.InnerText = xmlElement.InnerText;             
limitedWebPartManager.AddWebPart(contentEditor, webPartZone, 2);
limitedWebPartManager.SaveChanges(contentEditor); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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