Apr 10, 2012

Downloading Wsp from SharePoint Central Admin


Recently I got a requirement to check the contents of a wsp which was already deployed on the Client's SharePoint server.  I have given access to Central Admin, but as you know SharePoint will not provide us any link to download/extract the deployed wsp. 

These are the ways I found on the web to download/extract the wsp..

  • Using Power Shell Script
  •  Using Code

1. Using PowerShell script :




$myFarm = Get-SPFarm
$myWSP = $myFarm.Solutions.Item("documentspacenew.wsp").SolutionFile
$ myWSP.SaveAs("c:\purna\ documentspacenew.wsp")

 

This will be saved as 



 
2.  Using Code :

  protected void btnGetWSP_Click(object sender, EventArgs e)
        {
            SPSolutionCollection allSolutions = SPFarm.Local.Solutions;
            foreach (SPSolution mySolution in allSolutions)
            {
                SPPersistedFile myWSP = mySolution.SolutionFile;
                if (myWSP.Name == "documentspacenew.wsp")
                {
                    myWSP.SaveAs("c:\\Purna\\" + mySolution.Name);
                }
            }
        }

Here the SPSolutionCollection , SPPersistedFile classes are referred from   Microsoft.SharePoint.Administration namespace.

Finally I have got the contents of the downloaded solution by changing the extension from .wsp to .cab.
 

Hope Helpful..!

Apr 9, 2012

Adding Webpart to the Page Programatically in SharePoint

Hi All,

Here I am explaining you how to add a web part to the page programatically.  For this demo I have created a web part page called Demo.aspx  and I am adding a content editor web part to this page through programatically. 

For this first we need to get the reference of the SPLimitedWebPartManager  of the page where we are going to add the webpart.

Here is the code
protected void Addwebpart_Click(object sender, EventArgs e)
        {           
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    //getting reference of SPLimitedWebpartManager of the page
                    SPLimitedWebPartManager wpManager = web.GetLimitedWebPartManager("http://sp2010:9999/SitePages/Demo.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);                   
                    //Creation of ContentEdiotr Webpart
                    ContentEditorWebPart myCEWP = new ContentEditorWebPart();
                    myCEWP.ID = "myCEWP";
                    myCEWP.Title = "Demo Content Editor";
                    //Adding Webpart to the page
                    wpManager.AddWebPart(myCEWP, "Main", 1);
                    wpManager.SaveChanges(myCEWP);
                }
            }

        }

If you want to add the Shared Documents (ListView webpart) to the page,  Here is the way..

                    web.AllowUnsafeUpdates = true;
                    SPList lstCDD = web.Lists.TryGetList("Shared Documents");
                    if (lstCDD != null)
                    {
                        SPLimitedWebPartManager wpManager = web.GetLimitedWebPartManager("http://sp2010:9999/SitePages/Demo.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

                        XsltListViewWebPart webpartCDD = new XsltListViewWebPart();
                        webpartCDD.ListName = lstCDD.ID.ToString("B").ToUpper();
                        webpartCDD.ViewGuid = lstCDD.DefaultView.ID.ToString("B").ToUpper();
                        wpManager.AddWebPart(webpartCDD, "Main", 2);
                        wpManager.SaveChanges(webpartCDD);

                    }

Here the AddWebpart method expects three parameters, webpart, zoneid(string-Id of the webpartzone where the webpart is being added to like Main, Left etc), zone index(Int-Index of the zone like 1,2,3..)

Cheers,