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

I try to add by default a scheduling start date when I create a page or an article. There is my code :

 public override void ItemAdding(SPItemEventProperties properties)
 {

       base.ItemAdding(properties);

       SPListItem listItem = properties.ListItem;
       ScheduledItem scheduledItem = null;
       if (ScheduledItem.IsScheduledItem(listItem))
       {
           scheduledItem = ScheduledItem.GetScheduledItem(listItem);
       }
       else
       {
           throw new System.ArgumentException
             ("The document library containing this SPListItem must support scheduling",
             "listItem");
       }

       scheduledItem.StartDate = DateTime.Today;
       scheduledItem.ListItem.Update();

       scheduledItem.Schedule();

   }

But I have the error which said that listItem is null (because it's not create). So I need help. Can anyone provide some guidance? Thanks in advance

share|improve this question
    
Thanks for your help and sorry for the late of this message but I have an error when I try to execute my code with itemAdded : The file Pages/Article/testart.aspx has been modified by author at the line -> 'scheduledItem.ListItem.Update();' Any ideas? I think it's because when I create a page, the "edition mode" is activated so there is a conflict... –  Walloey Nov 21 '12 at 17:22

2 Answers 2

As you have noticed, the item has not yet been created in the ItemADDING event. Since you don't seem to need to have your code executed on the adding event, I suppose that you could just move it to the ItemAdded receiver, where the item should be normally available.

share|improve this answer
    
Thanks for your help but if I try this, I have the Exception : "The security validation for this page is invalid" when the code try to execute "scheduledItem.ListItem.Update();"... Any ideas? –  Walloey Nov 19 '12 at 14:57
    
Try to change the line scheduledItem.ListItem.Update(); to properties.Web.AllowUnsafeUpdate=true; scheduledItem.ListItem.Update(); properties.Web.AllowUnsafeUpdate=false; –  SPArchaeologist Nov 19 '12 at 15:14
    
Sorry for the late of this message but I have an error when I try to execute my code with itemAdded : The file Pages/Article/testart.aspx has been modified by author at the line -> 'scheduledItem.ListItem.Update();' Any ideas? –  Walloey Nov 21 '12 at 16:56

try it using "An Item Was Added" Event Receiver.

http://msdn.microsoft.com/en-us/library/gg252010(v=office.14).aspx

It will work.

share|improve this answer
    
Sorry for the late of this message but I have an error when I try to execute my code with itemAdded : The file Pages/Article/testart.aspx has been modified by author at the line -> 'scheduledItem.ListItem.Update();' Any ideas? –  Walloey Nov 21 '12 at 16:20
    
Replace "scheduledItem.ListItem.Update();" with "properties.ListItem.SystemUpdate();". This might Help you. –  Torque Nov 23 '12 at 7:12
    
That's great but if the user want change directly the option to Immediatly, he can't. So that's why I used ItemAdding. –  Walloey Nov 23 '12 at 9:14
    
I don't get What you want to say. –  Torque Nov 23 '12 at 11:05

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.