Tuesday, November 10, 2009

Sharepoint Object Model Programming #2

Developing and Deploying Custom Web Parts

Create a new C# project in Visual Studio, Select Class Library as Project Type. Name it as MyNewWebPart.

Assembly References - System.Web

Class Library References -
• System.Web;
• System.Web.UI;
• System.Web.UI.WebControls;
• System.Web.UI.WebControls.WebParts;

Code -
namespace MyNewWebPart
{
public class MyWelcomePart : System.Web.UI.WebControls.WebParts.WebPart
{
string _welcomeMessage = "!Welcome to My New Site!";
[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true),
WebDisplayName("Welcome Message"),
WebDescription("Welcome Message")
]
public string WelcomeMessage
{
get
{
return _welcomeMessage;
}
set
{
_welcomeMessage = value;
}
}

public override void RenderControl(HtmlTextWriter writer)
{
RenderChildren(writer);
}

protected override void CreateChildControls()
{
Table tbl = new Table();
TableRow tr = new TableRow();
TableCell td = new TableCell();
td.Font.Bold = true;
td.Text = this.WelcomeMessage;
tr.Controls.Add(td);
tbl.Controls.Add(tr);
this.Controls.Add(tbl);
}
}
}

Deployment -
  • There are two choices to deploy the assembly to the SharePoint directory.
    Deploy the assembly to the Assembly Folder (GAC)(assembly should be strong named).
    Put the assembly to the bin folder of the portal directory.

    For this example we will use 1st option.
  • Create a new SafeControl entry in web.config for our assembly as shown below. <SafeControls>
    .
    <SafeControl Assembly="MyNewWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=****key****" Namespace="MyNewWebPart" TypeName="*" Safe="True" />
    </SafeControls>
  • Reset IIS

Add WebPart on page
  • Go to Site Action - Site Setting - Web Parts
  • Click on new , select MyNewWebPart and click on Populate gallery
  • Site action- edit page - add a web part
  • Select MyWelcomePart from the list
  • Our new welcome part will be visible on page.

-------------------------------------------------------------------------------
Add a Web Part Using Object Model (Programmatically)

In This Example we will add our custom webpart & some default webpart(content editor & page viewer web part) using object model. Add a new webpart page MyPage1.aspx on your site(http://localhost/sites/MyNewSite) in 'Shared Document' document library.

Create a Console application and add following references and code.

Assembly References
• Windows SharePoint Services
• System.Web assembly
• MyNewWebPart assembly

Class Library References
• Microsoft.SharePoint
• Microsoft.SharePoint.WebControls;
• Microsoft.SharePoint.WebPartPages;
• System.Web
• MyNewWebPart


Code -

SPSite site = new SPSite("http://localhost/sites/MyNewSite");
SPWeb web = site.RootWeb;
web.AllowUnsafeUpdates = true;
SPLimitedWebPartManager webParts = web.GetLimitedWebPartManager("Shared Documents/MyPage1.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

//Create an instance of Custom Webpart and add in a Webpart zone
MyNewWebPart.MyWelcomePart wp = new MyWelcomePart();
wp.Title = "My WebPart Using OM";
webParts.AddWebPart(wp, "Left", 0);
webParts.SaveChanges(wp);

//Create an instance of CEWP and add in a Webpart zone
Microsoft.SharePoint.WebPartPages.ContentEditorWebPart cewp = new Microsoft.SharePoint.WebPartPages.ContentEditorWebPart();
XmlDocument xmlDoc = new XmlDocument();
System.Xml.XmlElement xmlElem = xmlDoc.CreateElement("xmlElem");
xmlElem.InnerText = "Hello From CEWP!";
cewp.Content = xmlElem;
cewp.Title = "My CEWP Using OM";
webParts.AddWebPart(cewp, "Left", 0);
webParts.SaveChanges(cewp);

//Create an instance of Page Viewer Webpart and add in a Webpart zone
Microsoft.SharePoint.WebPartPages.PageViewerWebPart pvwp = new Microsoft.SharePoint.WebPartPages.PageViewerWebPart();
pvwp.SourceType = PathPattern.URL;
pvwp.ContentLink = "http://www.google.com";
pvwp.Title = "My PVWP Using OM";
webParts.AddWebPart(pvwp, "Left", 0);
webParts.SaveChanges(pvwp);

web.Update();
web.Dispose();
site.Dispose();
-------------------------------------------------------------------------------
Import a Web Part Using XML file (.dwp or .webpart File)

Assembly References
• Windows SharePoint Services assembly

Class Library References
• System.Web library
• System.Xml library
• System.Web.UI.WebControls.WebParts.WebPart class

Code -

SPSite site = new SPSite("http://localhost/sites/MyNewSite");
SPWeb web = site.RootWeb;
web.AllowUnsafeUpdates = true;
SPLimitedWebPartManager webParts = web.GetLimitedWebPartManager("Shared Documents/MyPage1.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
System.Web.UI.WebControls.WebParts.WebPart wp;
XmlReader reader = XmlReader.Create("MyWelcomePart.webpart");
string strErrMsg;
wp = webParts.ImportWebPart(reader, out strErrMsg);
wp.Title = "My Webpart using xml";
webParts.AddWebPart(wp, "Left", 0);
webParts.SaveChanges(wp);
web.Update();
web.Dispose();
site.Dispose();


More - Sharepoint Object Model Programming

1 comment:

Anonymous said...

you dont need to check out the page ?