Re: show a list view in a customized web part
- From: Marty <marty.wassmer@xxxxxxxxx>
- Date: 14 May 2007 08:46:29 -0700
On May 11, 11:22 pm, <deltai...@xxxxxxxxxxxxxxxxx> wrote:
this does not help. Can you point to something in english"Laurent Cotton" <lcot...@xxxxxxxxxxxx> wrote in message
news:FE21AC0B-8187-4191-8971-86767F8DC81F@xxxxxxxxxxxxxxxx
Hi,
See the WebCast of TechDays 2007 at
http://www.microsoft.com/france/vision/WebCastMSDNTechDays.aspx?EID=B....
It's in French but Renaud Compte shows how to create a Web Part able to
query list using SPQuery and display result in a SPGridView.
--
Laurent Cotton
www.bewise.fr
<deltai...@xxxxxxxxxxxxxxxxx> wrote in message
news:eYdUOj9kHHA.4188@xxxxxxxxxxxxxxxxxxxxxxx
Hi
I am writing a web part where I want to show a contact list but use one
of the views define for that contact list. I do not want to use the list
view web part but basically want to write my own list view web part with
some UI in addition to what the list view will show. Can anyone tell me
how I can display a view associated with a list in a customized web part.
Thanks
Here's a simple list viewer I wrote... Hope this helps.
public class WebPartListViewer3 :
Microsoft.SharePoint.WebPartPages.WebPart
{
//var for the name of the list
private string _listName;
[Browsable(true)]
[Description("The name of the list you want to show")]
[FriendlyName("List Name")]
[WebPartStorage(Storage.Shared)]
public string listName
{
get { return _listName; }
set { _listName = value; }
}
//var for the name of the view
private string _viewName;
[Browsable(true)]
[Description("The name of the view you want to show")]
[FriendlyName("View Name")]
[WebPartStorage(Storage.Shared)]
public string viewName
{
get { return _viewName; }
set { _viewName = value; }
}
protected override void Render(HtmlTextWriter writer)
{
try
{
if (_listName == null)
{
writer.Write("Please Set List Name");
}
else
{
string[] pathSplit = _listName.Split('/');
SPWeb root = SPControl.GetContextWeb(Context);
int x = 0;
while (x < pathSplit.Length-1)
{
if (pathSplit[x] != "")
{
root = root.Webs[pathSplit[x]];
}
x++;
}
SPList finalList = root.Lists[pathSplit[x]];
SPView collView = finalList.DefaultView;
SPListItemCollection Coll =
finalList.GetItems(collView);
writer.Write("<table border=1 cellpadding=2
cellspacing=0 width=100% align=center>");
string dView;
if (_viewName == null)
{
dView = finalList.DefaultView.Title;
}
else
{
dView = finalList.Views[_viewName].Title;
}
string[] temp;
string fieldType;
//list fields
int cols = 1;
StringBuilder header = new StringBuilder();
header.Append("<tr>");
foreach (string field in collView.ViewFields)
{
if (field.Replace("_x0020_", " ") != "Edit")
{
header.Append("<td><b>" +
field.Replace("_x0020_", " ") + "</td>");
cols++;
}
}
header.Append("</tr>");
//link to list above header
writer.Write("<tr>");
writer.Write("<td colspan=" + --cols + "><b><a
href=" + ((string)finalList.DefaultViewUrl.ToString()).Replace(" ",
"%20") + ">" + finalList.Title.ToString() + "</a>");
writer.Write("</td></tr>");
//writes header
writer.Write(header.ToString());
//no items error mess
if (Coll.Count == 0)
{
writer.Write("<tr>");
writer.Write("<td colspan=" + cols + "><b>No
Items in " + _listName + " list</td>");
writer.Write("</tr>");
}
//list items
object fieldObject;
string fieldData;
string url, desc;
int rowNum = 1;
foreach (SPListItem item in Coll)
{
writer.Write("<tr>");
foreach (string field in collView.ViewFields)
{
writer.Write("<td>");
try
{
//debug
//writer.Write("<td>Field Name" +
field + "</td>");
fieldObject = item[field];
//
writer.Write(item.Fields.GetFieldByInternalName(field).PrimaryPIAttribute);
if (fieldObject == null)
{
fieldData = "";
}
else
{
fieldData =
fieldObject.ToString();
}
fieldType =
finalList.Fields.GetFieldByInternalName(field).TypeAsString;
//debug
//writer.Write(fieldType);
switch (fieldType)
{
case "URL":
temp = fieldData.Split(',');
url = temp[0];
desc = temp[1];
temp = url.Split('?');
temp = temp[0].Split('.');
if (desc == "") { desc = "n/
a"; }
if (temp[temp.Length-1] ==
"gif" || temp[temp.Length-1] == "jpg" || temp[temp.Length-1] == "bmp")
{
writer.Write("<img
alt='"+desc+"' src='" + url + "'>");
}
else
{
writer.Write("<a href='" +
url + "'>" + desc + "</a>");
}
break;
case "Lookup":
temp = fieldData.Split('#');
writer.Write(temp[1]);
break;
case "User":
if (fieldData != "")
{
temp =
fieldData.Split('#');
//shows user presence
//SPUser user =
SPControl.GetContextWeb(Context).AllUsers.
//writer.Write("<span><img
border=\"0\" height=\"12\" width=\"12\" src=\"/_layouts/images/
blank.gif\" onload=\"IMNRC('" + item["ID"] + "')\" id=\"IMID" + rowNum
+ "\" ShowOfflinePawn=1>");
writer.Write(temp[1]);
//writer.Write("</span>");
}
else
{
writer.Write("n/a");
}
break;
case "Number":
if (fieldData == "")
{
writer.Write("n/a");
}
else
{
writer.Write(fieldData);
}
break;
case "DateTime":
if (fieldData == "")
{
writer.Write("n/a");
}
else
{
temp = fieldData.Split('
');
writer.Write(temp[0]);
}
break;
case "Computed":
temp = fieldData.Split(' ');
if (!collView.ReadOnlyView)
{
//edit button
//writer.Write(fieldData);
//writer.Write("<a
href=http://ithomedev/SteeringCommittees/Executive/Lists/TopProjects/
EditForm.aspx?ID=" + item["ID"] + "><img border=0 src=_layouts/images/
edititem.gif></a>");
}
break;
default:
writer.Write(fieldData);
break;
}
}
catch (Exception e)
{
writer.Write("Error in " + field);
writer.Write(" Stack " + e.Message);
}
writer.Write("</td>");
}
writer.Write("</tr>");
rowNum++;
}
writer.Write("</table>");
}
}
catch (Exception e)
{
writer.Write("Error:" + e.Message + "<br>");
writer.Write("Error:" + e.StackTrace + "<br>");
writer.Write("Error:" + e.Source + "<br>");
}
}
}
.
- References:
- show a list view in a customized web part
- From: deltainfo
- Re: show a list view in a customized web part
- From: deltainfo
- show a list view in a customized web part
- Prev by Date: Re: Adding a second web part to a calendar's calendar.aspx on creation
- Next by Date: RE: SharePoint - WebParts - Availability
- Previous by thread: Re: show a list view in a customized web part
- Next by thread: RE: SharePoint - WebParts - Availability
- Index(es):