domenica 21 agosto 2011

Getting Started: Building Web Parts in SharePoint 2010

Link

Il codice seguente mostra come modificare la proprietà Text di una Lable quando viene caricata una “Visual Web Part”.

NOTA: Questo codice si presuppone non vi è un controllo ASP.NET Label chiamati messaggio in

Visual Web Part

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace TestVisualWebParts.VisualWebPart1
{
public partial class VisualWebPart1UserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
message.Text = "Welcome to SharePoint 2010 Development";
}
}
}

Il codice seguente mostra come “mostrare” un messaggio di benvenuto quando viene carichata una Web Part:

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace TestVisualWebParts.BonnevilleStandardWP
{
[ToolboxItemAttribute(false)]
public class TestVisualWebPartsWP : WebPart
{
public TestVisualWebPartsWP()
{
}

protected override void CreateChildControls()
{
string username = SPContext.Current.Web.CurrentUser.LoginName;
LiteralControl myMessage = new Literalcontrol("

Welcome " + userName + " to SharePoint 2010 Development

");
this.Controls.Add(myMessage);
base.CreateChildControls();
}

protected override void RenderContents(HtmlTextWriter writer)
{
base.RenderContents(writer);
}
}
}



Il codice seguente mostra come scorrere tutti gli elenchi e Web secondari in un sito di SharePoint, e aggiungere a un controllo TreeView in Visual Web Part.

Note: Questo codice presuppone che ci sia un controllo TreeView denominato siteStructure in Visual Web Part.

Si noti inoltre come il addWebs () viene chiamato il metodo ricorsivo.

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Web;


namespace TestVisualWebParts.Bonneville
{
public partial class usTetWP: UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
SPWeb thisWeb = null;
TreeNode node;
thisWeb = SPContext.Current.Web;
//Add the Web's title as the display text for the tree node, and add the URL as the NavigateUri
node = new TreeNode(thisWeb.Title, null, null, thisWeb.Url, "_self");
//The Visual Web Part has a treeview control called siteStructure
siteStructure.Nodes.Add(node);
//Get a reference to the current node, so child nodes can be added in the correct position
TreeNode parentNode = node;
//Iterate through the Lists collection of the Web
foreach (SPList list in thisWeb.Lists)
{
if (!list.Hidden)
{
node = new TreeNode(list.Title, null, null, list.DefaultViewUrl, "_self");
parentNode.ChildNodes.Add(node);
}
}
foreach (SPWeb childWeb in thisWeb.Webs)
{
//Call our own helper function for adding each child Web to the tree
addWebs(childWeb, parentNode);

childWeb.Dispose();
}
siteStructure.CollapseAll();
}



void addWebs(SPWeb web, TreeNode parentNode)
{
TreeNode node;
node = new TreeNode(web.Title, null, null, web.Url, "_self");
parentNode.ChildNodes.Add(node);
parentNode = node;
foreach (SPList list in web.Lists)
{
if (!list.Hidden)
{
node = new TreeNode(list.Title, null, null, list.DefaultViewUrl, "_self");
parentNode.ChildNodes.Add(node);
}
}
foreach (SPWeb childWeb in web.Webs)
{
//Call the addWebs() function from itself (i.e. recursively)
//to add all child webs until there are no more to be added
addWebs(childWeb, parentNode);
}

}
}
}



The following code shows how to use a DateTimeControl and ListViewByQuery control to display tasks that are due before the date chosen by the user. Il codice seguente mostra come utilizzare un DateTimeControl e un controllo ListViewByQuery per visualizzare l’elenco delle attività nelle data indicata per lo user corrente.

Nota: La DateTimeControl include un gestore di eventi che “refresh” la definizione di query per il controllo ListViewByQuery quando l'utente seleziona una data diversa.


protected override void CreateChildControls()
{
SPWeb thisWeb = null;
DateTimeControl filterDate = new DateTimeControl();
filterDate.DateOnly = true;
filterDate.AutoPostBack = true;
thisWeb = SPContext.Current.Web;
filterDate SelectedDate = DateTime.Today;
filterDate.DateChanged += new EventHandler(filterDate_DateChanged);
this.Controls.Add(filterDate);
MyCustomView = new ListViewByQuery();
MyCustomView.List = thisWeb.Lists["Tasks"];
SPQuery query = new SPQuery(MyCustomView.List.DefaultView);
query.ViewFields = "";
//CAML Query. Note that there must be a using statement for
//Micoroft.SharePoint.Utilities to use SPUtility class as shown
query.Query = ""
+ ""
+ SPUtility.CreateISO8601DateTimeFromSystemDateTime(FilterDate.SelectedDate)
+"
";
MyCustomView.Query = query;
this.Controls.Add(new LiteralControl("
"));
this.Controls.Add(MyCustomView);
EnsureChildControls();
base.CreateChildControls();
}
void filterDate_DateChanged(object sender, EventArgs e)
{
DateTimeControl filterDate = (DateTimeControl)sender;
SPQuery query = new SPQuery(MyCustomView.List.DefaultView);
query.ViewFields = "";
query.Query = ""
+ ""
+ SPUtility.CreateISO8601DateTimeFromSystemDateTime(FilterDate.SelectedDate)
+ "
";
MyCustomView.Query = query;

Nessun commento:

Posta un commento