As mentioned above, I like Arsalans approach very much, but for some reasons (i'm doing this project for my bachelor thesis at the moment and as mentioned before it's my first time doing sharepoint dev. so I am fairly new to all of this and have no clue if the following is goodor bad ;) ) the head-of-development (who is my auditor for the thesis, too) liked my approach better because it's more "autarkical", as he said, so I wanted to share my solution with you, too. This is just the disclaimer: I personally would do it arsalans way ;)
But nonetheless, here's the code (behind) of my visual webpart:
Page_Load
Here I'm adding an onclick-javascript handler to my asp.net Button (Names are changed for better understanding) which opens a new browser page / tab.
protected void Page_Load(object sender, EventArgs e)
{
try
{
...
SPList attendeeList = web.Lists["Attendees"];
SPListItem currentEventItem = (SPListItem)SPContext.Current.Item;
SPFieldLookupValue eventLookup = new SPFieldLookupValue(currentEventItem.ID, currentEventItem .Title);
ShowAttendeeListButton.Attributes.Add("onclick", "javascript: var win=window.open(); self.focus();win.document.open();win.document.write('');win.document.write('" + GetAttendeeList() + "');win.document.write('');win.document.close();");
}
catch (Exception ex)
{
...
}
}
GetAttendeeList
Here I'm formatting the newly opened Page(removed try/catch for better readability). The css is stored into a document library called "scripts" in my site Collection. (will get to style library later on)
private string GetAttendeeList() {
string DocType = "<!doctype html>";
string Metadata = "<html><head><title>Attendee List for Event " + currentEventItem["Title"].ToString() + "</title><link rel=\"stylesheet\" type=\"text/css\" href=\"../../scripts/tablestyle.css\"/></head><body>";
string Tableheader = "<table id=\"hor-minimalist-b\" summary=\"Attendee List\"><thead><tr><th scope=\"col\">Title</th> <th scope=\"col\">Name</th> <th scope=\"col\">E-Mail</th> <th scope=\"col\">Department</th><th scope=\"col\">Phone</th></tr> </thead>";
string Tablebody = "<tbody>" + GetUsersForAttendeeList() + "</tbody></table>";
string PrintButton = "<input type=\"button\" value=\"Print List\" onClick=\"window.print()\">";
string EndHtml = "</body></html>";
string ListResult = DocType + Metadata + Tableheader + Tablebody + PrintButton + EndHtml;
return ListResult;
}
GetUsersForAttendeeList (& CheckIfSPFieldIsNull)
Here I'm getting the Users for the actual event formatted for my tablecells. The Event-Column is a lookup-column in my attendees-list, that's why there's a "eventLookup" in page_load. The method CheckIfSPFieldIsNull is needed for there could be empty fields in the attendee List.
private string GetUsersForAttendeeList()
{
string res = null;
var foundAttendees = from SPListItem item in attendeeList.Items
where item["Event"].ToString() == eventLookup.ToString()
select item;
foreach (var attendee in foundAttendees)
{
res += "<tr>";
//Done this for it's a User/Group Field, having ID;#LoginName stored (yeh, I know, LoginName sucks, but that_'s how it's done in productive env. here ;) )
string attendeeName = CheckIfSPFieldIsNull(attendee["Name"]).Split('#').Last();
res += CheckIfSPFieldIsNull(attendee["Title"]) + "<td>" + attendeeName;
res += CheckIfSPFieldIsNull(attendee["E-Mail"]) + CheckIfSPFieldIsNull(attendee["Department"]) + CheckIfSPFieldIsNull(attendee["Phone"]);
res += "</tr>";
}
return res;
}
}
private String CheckIfSPFieldIsNull(object field) {
if (field == null)
{
return "<td>N/A</td>";
}
else
{
return "<td>" + field.ToString() + "</td>";
}
}
So yeah, that's it. I hope you can understand what I've done. Feel free to ask me if something's unclear, but please use the answer above instead (and please don't kill me for this is a very cheesy approach in my opinion. Just wanted to show it for completeness, and in fact, it IS an answer. Not the best, though ;) )
Goodie: tablestyle.css-content (works in actual versions of IE/FF/Chrome/Opera): http://pastebin.com/1wGDbt2H