I'm creating a 'Contact Us' webpart with form to allow anonymous user to send a message which will be added to a SharePoint List.
However I cannot grant the anonymous user the permission to Add element to List (and therefore the permission to View the List) because he isn't supposed to see all of the messages that were previously sent by other people.
I've tried to use the following code:
SPSite siteContext = SPContext.Current.Site;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedSite = new SPSite(siteContext.ID))
{
using (SPWeb web = elevatedSite.RootWeb)
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists.TryGetList("Messages");
Guid listGuid = list.ID;
if (listGuid != null)
{
RichTextField MessageField = new RichTextField();
MessageField.ControlMode = SPControlMode.New;
MessageField.ListId = listGuid;
MessageField.FieldName = "Message";
this.MessagePlaceholder.Controls.Add(MessageField);
}
web.AllowUnsafeUpdates = false;
}
}
});
But when the anonymous user proceeds to the page, the MessageField isn't rendered at all.
I guess it's because the code is pointing the field to a list which the user doesn't have access to.
What am I doing wrong? Am I missing something in my code with RunWithElevatedPrivileges? Any help would be appreciated.