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'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);
                }
share|improve this question
    
Can you post the code you're using to add the web part? –  Nigel Whatling Jul 4 '12 at 14:43
    
sure................... –  fallout Jul 5 '12 at 13:01
    
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 –  Hristo Yankov Feb 25 at 11:40
add comment

2 Answers 2

up vote 1 down vote accepted

fallout,

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

Hope that it helps,

share|improve this answer
    
Obrigado Daniel! –  fallout Oct 3 '12 at 14:49
add comment

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); 
share|improve this answer
add comment

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.