0

In my project, I want to get all content of a SPListItem which is a calendar event. However, I have no idea to get them.

 using (SPSite site = new SPSite(@"http://spabc/"))
            using (SPWeb web = site.OpenWeb())
            {
                SPList cal = web.Lists["Calendar"];
                SPListItemCollection items = cal.Items;
                string texts = string.Empty;
                foreach (SPListItem item in items)
                {
                    var xml = item.Xml;
                    texts += xml;

                }
                lab.Text = "Event: " + texts;// it shows "Event: "
            }

Target:

Title, start Time, End Time, All Day Event, and Recurrence..

example of recurrence:

Recurrence: Every 1 month(s) on the first Tuesday

2
  • I had the same problem some time ago - the recurrence field is stored in XML format as you may see in the current answer. You can either try to parse it manually, but know that you would deviate from SharePoint internal working (if they change the "readable version" of the message, your code wouldn't reflect that) or try to reuse the same code SharePoint use. See this answer here - sadly, this requires some workaround since it doesn't seem Microsoft built that function for easy reuse. Commented Nov 1, 2016 at 13:11
  • @SPArchaeologist my project is a web part project. And i cannot get the XML of it, but console program and application page word very well..
    – king yau
    Commented Nov 2, 2016 at 2:50

1 Answer 1

0

To get recurrence data from a calendar list, you have to fetch the data from RecurrenceData field. It will return an Xml formatted string. Then parse the xml to read the content such as:

     using (SPSite site = new SPSite(@"http://spabc/"))
        using (SPWeb web = site.OpenWeb())
        {
            XmlDocument xmlDoc = new XmlDocument();
            string recurrenceData = "";
            SPList cal = web.Lists["Calendar"];
            SPListItemCollection items = cal.Items;
            string texts = string.Empty;
            foreach (SPListItem item in items)
            {
                if(item["RecurrenceData"] != null)
                {
                    recurrenceData = item["RecurrenceData"].ToString();
                    xmlDoc.LoadXml(recurrenceData);
                    string xpath = "recurrence/rule";
                    var nodes = xmlDoc.SelectNodes(xpath);
                    foreach (XmlNode childrenNode in nodes)
                    {
                        Console.WriteLine("Event: " + item["Title"].ToString());
                        Console.WriteLine("Start from " + childrenNode.SelectSingleNode("//firstDayOfWeek").InnerText);
                        Console.WriteLine("Every " + childrenNode.SelectSingleNode("//repeat/daily").Attributes["dayFrequency"].Value + " days");
                        Console.WriteLine("Ended on " + childrenNode.SelectSingleNode("//windowEnd").InnerText);
                    }
                }
            }
        }
8
  • how about recurrence?
    – king yau
    Commented Nov 1, 2016 at 3:49
  • Please check my edited answer Commented Nov 1, 2016 at 3:50
  • it return System.Boolean for the recurrence.. so fail
    – king yau
    Commented Nov 1, 2016 at 4:04
  • I've just tested the above code and add the recurrence field: texts += item["fRecurrence"].ToString(); and it worked. Please check my updated answer, I have included the Recurrence field there.. Commented Nov 1, 2016 at 4:12
  • 1
    Next time please be more specific about what you are currently trying to accomplish on the title and your question. Please check my updated answer. Commented Nov 1, 2016 at 5:22

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