Ok so I have a blog site and I need to change the name of the author of each post to 'Anonymous' after he saves the post. I created an EventReceiver project in Visual Studio and gave the URL "http://sharepoint/subsite/blog"...does this mean that the event receiver will be applied to all lists under the blog site?
I know there is a Posts and a Comments lists for the blog site and I would like the author to both of the lists to be changed to 'Anonymous' once a post is submitted.
So this is the code I have come up with:
public override void ItemAdded(SPItemEventProperties properties)
{
properties.ListItem["Created By"] = "Anonymous";
properties.ListItem.Update();
base.ItemAdded(properties);
}
Now I have no idea why this isn't working, I know its attached to the correct site but when I create a blog post or comment the author does not change...as a test I even tried changing the Title instead of the Created By field but still nothing. (I am running the project using F5 and the correct blog is opening up correctly).
From what I've read the properties.ListItem should have a 'New Value' in the ItemAdded event so I don't think its a null issue.
Any ideas how I can fix this?
EDIT: As a test I replaced my code in ItemAdded with this, and attached to a list (not on the blog, just a random list):
SPListItem currentItem = properties.ListItem;
currentItem["Title"] = DateTime.Now;
currentItem.Update();
This changes the Title to the current date and time but when I try the same with "Created By" instead of "Title" it doesn't work....is there a different name for the Created By column? Also if I attach this same EventReceiver to the Blog site, the title doesn't change for either the posts or comments lists....I'm so confused about this now...
EDIT #2: Ok I'm getting closer but I have a problem. So I created a SharePoint group called Anonymous with no users in it. Now below is the code I have:
using (SPSite site = new SPSite("http://test-sharepoint/my list/"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["Posts"];
SPListItemCollection listItemCollection = list.Items;
foreach (SPListItem listItem in listItemCollection)
{
SPFieldUserValue userName = new SPFieldUserValue(web, 22, "Anonymous");
listItem["Author"] = userName;
listItem["Editor"] = userName;
listItem.Update();
}
web.Update();
}
}
OK, now the problem...I tested this from a console application in Visual Studio and it works fine, EXCEPT that I need to execute the Visual Studio program everytime I want the Created By field to change, otherwise any items added after will not be updated until I re-execute the console application.
I'm guessing this is because I should be using an EventReceiver project? But how would I modify this code to work in an EventReceiver project....since I specify the URL at the creation of the project but I can't specify the exact list...how can I use web.Lists like above?
EDIT #3: PROGRESS!!!! Ok so I now have the code working fine on a list, however, I can't seem to get it working on the Blog Posts or Comments lists...any idea what is different about these lists compared to a Custom List?
This is my code now:
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
SPSite site = new SPSite("http://test-sharepoint/hr/blog/"); //SPContext.Current.Site;
SPWeb web = site.OpenWeb();
SPFieldUserValue userName = new SPFieldUserValue(web,22,"Anonymous");
SPListItem currentItem = properties.ListItem;
//currentItem["Title"] = userName; //DateTime.Now.ToString();
currentItem["Author"] = userName;
currentItem["Editor"] = userName;
currentItem.SystemUpdate();
}