Nil Pun
Nil Pun

Reputation: 17373

Sharepoint custom Web Part

I've a got a code snippet below modified from http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webpartpages.webpart.aspx :

public class WebPartBla : Microsoft.SharePoint.WebPartPages.WebPart
{
    private ArrayList someList;

    protected override void CreateChildControls()
    {
        someList = new ArrayList();

        SPWeb myWeb = SPControl.GetContextWeb(this.Context);
        foreach(SPList list in myWeb.Lists)
        {
            if (list.BaseTemplate == SPListTemplateType.Tasks)
            {
                someList(list.Description);
            }
        }
    }

    /// <summary>
    /// Render this Web Part to the output parameter specified.
    /// </summary>
    /// <param name="output"> The HTML writer to write out to </param>
    protected override void RenderWebPart(HtmlTextWriter output)
    {
        string strHTML = "";
        for (int i = 0; i < someList.Count; i++)
        {
            strHTML = strHTML + "The task " + someList.Description + "<BR><BR>";
        }
        output.Write(strHTML);
    }
}

Could anyone please help me with

  1. how to use this code as SharePoint Web Part?
  2. how to deploy this to the sharepoint as web part?
  3. how does the RenderWebPart method above gets used?

Upvotes: 0

Views: 2736

Answers (1)

undefined
undefined

Reputation: 34238

Basically you need visual studio and SharePoint on the same machine to do any development.

Create an empty sharepoint project then add a webpart to the project. Put the code you have there into the webpart code file and build the solution (you will need to specify the URL of your local SP to do this). You can then deploy via the right click menu. An additional component of this build is a WSP file which you can take and deploy to other sharepoint environments.

THe renderWebPart menthod here allows you to directly write HTML for the webpart in code. Another option here is to use a visual web part.

heres a good tutorial (with pictures) showing how to create a webpart http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2010/02/15/intro-to-sharepoint-2010-development-how-to-build-and-deploy-a-web-part.aspx

Upvotes: 1

Related Questions