my use-case:
I'm trying to implement an event-management system to sharepoint using visual web part(s).
I have two custom lists, one is called "events". Let's say it has a column "Title" which shows the Event Details in a Displayform (the out-of-the-box title field it is, actually) and some other non-relevant details in other columns.
The second List is called "Attendees". There are two columns, first: "Attendee" (user field getting the loginname of the current user). Second: "Event" (at the moment: String of the Event title).
At my visual webpart (added via &ToolPaneView=2 above display form of the "Events"-List) I show buttons ("register" and "unregister") depending on the following code:
...
using System.Linq;
...
//Snippet of prerequisites for better understanding
SPWeb web = SPContext.Current.Web;
SPUser currentUser = web.CurrentUser;
SPListItem currentEventItem = (SPListItem)SPContext.Current.Item;
string userString = currentUser.ID.ToString() + ";#" + currentUser.LoginName.ToString();
...
//called in page_load
private void GetButtonView()
{
try
{
bool foundUsers = (from SPListItem item in tn.Items
where item["Attendee"].ToString() == userString
&& item["Event"].ToString() == currentEventItem.Title
select item).Count() > 0;
if (foundUsers)
{
...
ButtonRegister.Visible = false;
}
else
{
...
ButtonUnregister.Visible = false;
}
}
catch (Exception ex)
{
...
}
}
Problem at this use-case: If the event title ("Title") is changed later on in the events-list, I don't get a correct mapping attendee => event because at the moment I check only for matching of title-strings.
Preferred Solution: Make the "Event" Column at the Atendees-List a lookup column which gets the actual Event (ID and the title, I think) and shows the title in the "Attendees" List "Event"-column, so if the title is changed later on in the "Events"-List, the attendees-list will automatically update the referring entries in its "Event"-Column and I can show the "Register"/"Unregister"-Button (apart from having a correct mapping, which is necessary for sure ;) ).
It would be great if someone could give me a solution/hint how to do this programmatically for I don't get it to work.
Best regards, Dominik
p.s. I am german so I hope you understand my question. Feel free to ask for more details! I'll try my best to explain it in proper english.
edit 1: Here's my so-far addAttendee-method, too:
private void AddAttendee() {
try
{
//Using this because read-permissions are used for attendees
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite ElevatedSite = new SPSite(site.ID))
{
using (SPWeb ElevatedWeb = ElevatedSite.OpenWeb(web.ID))
{
SPList attendeeList= ElevatedWeb.Lists["Attendees"];
SPListItemCollection listItems = attendeeList.Items;
SPListItem item = listItems.Add();
item["Name"] = currentUser.ID.ToString() + ";#" +
currentUser.LoginName.ToString() + ";#";
item["Event"] = currentEventItem["Title"];
ElevatedWeb.AllowUnsafeUpdates = true;
item.Update();
ElevatedWeb.AllowUnsafeUpdates = false;
...
}
}
});
}
catch (Exception ex)
{
...
}
}