Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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();
   }
share|improve this question
add comment

1 Answer

The Created By Column is a User or Group column, it's not just plain text. I'm even sure if you can alter the contents of that column due to it being a record of who created the item in question.

If you hooked into ItemAdding you maybe able to set it to a predefined user, who you could give the display name of Anonymous....

share|improve this answer
    
You are right! I actually found this blogs.msdn.com/b/sowmyancs/archive/2008/03/14/… and it shows how you can change the author, however, I can only change it to a group of users not an actual user account. I'm thinking of just creating a group called Anonymous and calling it quits there. What do you think? This is the line that gets the user/group information and modifying the 14 is what changes the group SPFieldUserValue oUser = new SPFieldUserValue(oWeb, oWeb.CurrentUser.ID, oWeb.CurrentUser.LoginName); –  Tudor Sep 20 '12 at 15:53
    
I kind of figured it out...please refer to my EDIT #2 in the original post, maybe you can provide some help with where I am at now... –  Tudor Sep 20 '12 at 16:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.