Share via


Set maximum events for SharePoint calendar

Question

Friday, May 18, 2012 3:19 PM

Is there a way we can set maximum number of events, like 10, for sharepoint calendar per day? If the events are more that 10 for a day, sharepoint won't allow more events input.

Thanks!

All replies (9)

Monday, May 21, 2012 9:02 PM ✅Answered

you can loop by using a foreach on the List.Items and use the created field of the item, but that is not really best practice.

try to use a specfic SPQuery that returns only the items that are created the same date that the new event is added and check the amount of items this query results...

blog: http://www.tomvangaever.be


Tuesday, May 22, 2012 7:53 PM ✅Answered

you can loop by using a foreach on the List.Items and use the created field of the item, but that is not really best practice.

try to use a specfic SPQuery that returns only the items that are created the same date that the new event is added and check the amount of items this query results...

blog: http://www.tomvangaever.be

Thanks, Tom. SPQuery works!

I post my code here 

       SPList calist = properties.OpenWeb().Lists[properties.ListTitle];

               SPQuery query = new SPQuery();

               string dateinfo = properties.AfterProperties["EventDate"].ToString();
               string DateInfo = SPUtility.CreateISO8601DateTimeFromSystemDateTime(Convert.ToDateTime(dateinfo));

               query.Query = string.Format("<Where><Eq><FieldRef Name=\EventDate\ /><Value Type=\DateTime\>{0}</Value></Eq></Where>", DateInfo);

               SPListItemCollection listItems;
               listItems = calist.GetItems(query);
               int counter = listItems.Count;

               if (counter >= 2)
               {
                   properties.Cancel = true;
                   properties.Status = SPEventReceiverStatus.CancelNoError;
               }


Friday, May 18, 2012 4:22 PM | 1 vote

Hi!

Click the “Calendar View” option and configure your view in order to query what you want

MVP Office Development, MCP, MCTS SharePoint 2010 Development


Friday, May 18, 2012 6:41 PM

Hi!

Click the “Calendar View” option and configure your view in order to query what you want

MVP Office Development, MCP, MCTS SharePoint 2010 Development

you mean the modify view option in ribbon? I saw filter option where I could add query to my view. But I still need find a way to control maximum events input per day. For example, if the events reach the maximum number for a day, the new event option from ribbon or add sign will be disabled for the day.

thanks

 


Monday, May 21, 2012 6:38 AM

Hi Leicd,

You can use a column to record how many event has been created today. Then use list Validation to make sure the value of this column should less than 10 or something else.

Thanks

Pengyu Zhao

TechNet Community Support


Monday, May 21, 2012 4:04 PM

Hi Leicd,

You can use a column to record how many event has been created today. Then use list Validation to make sure the value of this column should less than 10 or something else.

Thanks

Pengyu Zhao

TechNet Community Support

Hi Pengyu Zhao,

Thanks for your reply. How Can I use a column to record how may events had been created for a day? A calculated field? Can you please specify?


Monday, May 21, 2012 7:24 PM

I would go for a list item event receiver that performs the validation.
http://msdn.microsoft.com/en-us/library/ms437502.aspx

Make your solution generic:
don't put 10 as hardcoded value, but retrieve it from the parent web Properties (SPWeb.Properties) and us the ID of the list as key.

blog: http://www.tomvangaever.be


Monday, May 21, 2012 8:17 PM

Hi Tom, I created an event receiver and here's my code

public override void ItemAdding(SPItemEventProperties properties)

{

base.ItemAdding(properties);

if (properties.ListTitle.Trim().ToString() == "MyCalendar")

{ if (properties.List.ItemCount > 2)

{ properties.Cancel = true;

properties.Status = SPEventReceiverStatus.CancelNoError;

}

}

}

But the question is, the properties.List.ItemCount counts all list items, how I can count items by date. Because I need to restrict daily events input. Thanks for your time


Friday, August 4, 2017 9:58 AM

Hhope someone will read my reply after 5 years...

This is exactly what I need for my SP Calendar. Guess the code will do, but.. where do I paste it? In the New Entry form? Is there anything in the code that I need to modify (like give it a different name or anything)?

regards

czechirus