none
Add a Event in Calendar using php

    Question

  • Hi,

    In my SharePoint calendar list name is Schedule. For add a event we need 3 required fields, Such as Title, Start Time and End Time. I tried to add a list item by using bellow code, but did't get any response. Can you please suggested any ideas.

    I don't know what is the problem here?

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
    <UpdateListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
    <listName>Schedule</listName>
    <updates>
    <Batch>
    <Method ID="1" Cmd="New">
    <Field Name="Title">SomeTitle</Field>
    <Field Name="Start Time">10-09-2013T12-32Z</Field>
    <Field Name="End Time">10-09-2013T12-55Z</Field>
    </Method>
    </Batch>
    </updates>
    </UpdateListItems>
    </soap:Body>
    </soap:Envelope>'

    Thanks,

    Printo Alexander

    Wednesday, October 09, 2013 6:03 AM

Answers

  • Yes, if you are able to create content in calendar list via UI then permission is fine. I have also tested below code in console application and working fine:

    DefaultService.Lists SPService = new DefaultService.Lists();
    SPService.Credentials = System.Net.CredentialCache.DefaultCredentials;
    System.Xml.XmlNode ndListView = SPService.GetListAndView("Test", "");
    string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value;
    string strViewID = ndListView.ChildNodes[1].Attributes["Name"].Value;
    
    XmlDocument doc = new XmlDocument();
    System.Xml.XmlElement batchs = doc.CreateElement("Batch");
    batchs.SetAttribute("OnError", "Continue");
    batchs.SetAttribute("ListVersion", "1");
    batchs.SetAttribute("ViewName", strViewID);
    
    batchs.InnerXml = "<Method ID='1' Cmd='New'><Field name='Title'>Addedfromservice</Field><Field Name='EventDate'>" + DateTime.Now + "</Field><Field Name='EndDate'>" + DateTime.Now.AddHours(1) + "</Field></Method>";
                try
                {
                    XmlNode ndReturn = SPService.UpdateListItems(strListID, batchs);
                    lblMsg.Text = ndReturn.OuterXml;
                }


    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    Friday, October 11, 2013 11:48 AM
    Moderator

All replies

  • Hello,

    For calendar, you need to use EventDate and EndDate. Try below code:

    <Field Name='Title'>New Test</Field>"
    <Field Name="EventDate">2010-05-05T12:00:00Z</Field>
    <Field Name="EndDate">2010-05-14T12:00:00Z</Field>
    http://social.msdn.microsoft.com/Forums/sharepoint/en-US/7c779d99-cf58-42cd-a86e-44e88c40fcd7/recurrence-display-render-failed-when-using-listsasmx-updatelistitems-method

    If still face issue then use list GUID instead of name in <ListName> (You can get list GUID from site-->list settings-->look at URL)

    http://www.codeproject.com/Tips/43928/Create-a-sharepoint-list-item-using-javascript-usi

    Let us know your result


    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, October 09, 2013 6:38 AM
    Moderator
  • Hi Thanks for your reply,

    This is my code. I don't know what happened here?

    what is Method id? How can i get it?

    Is this correct? Because i am new for SharePoint.

    <?php
    
    $login = 'username:password';
    
    $site = 'siteurl/Lists.asmx';  
    
    $xml = 'POST /_vti_bin/Lists.asmx HTTP/1.1
    Host: portal.myteachme.com
    Content-Type: text/xml; charset=utf-8
    Content-Length: 500
    SOAPAction: "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems"
    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
    <UpdateListItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
    <listName>Schedule</listName>
    <Batch OnError="Continue">
    <Method ID="0" Cmd="New">
    <updates>
    <Field Name="Title">New Test</Field>
    <Field Name="EventDate">2013-10-09T12:00:00Z</Field>
    <Field Name="EndDate">2013-10-09T01:00:00Z</Field>
    </Method>
    </Batch>
    </updates>
    </UpdateListItems>
    </soap:Body>
    </soap:Envelope>';
    
    	// Initialize session and set URL.
    	$ch = curl_init();
    
    	//set up url and login
    	curl_setopt($ch, CURLOPT_URL, $site);
    	curl_setopt($ch, CURLOPT_USERPWD, $login);
    	curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    
    	//set up ssl
    	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    	
    	//set up redirect and transfer
    	curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    	//make post
    	curl_setopt($ch, CURLOPT_POST, 1);
    	curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
    
    	// Get the response
    	$response = curl_exec($ch);
    
    	//check for errors
    	if(curl_errno($ch))
    	{
    		echo "error#: ".curl_errno($ch).'<br/>';
    		echo "error: ".curl_error($ch).'<br/>';
    	}
    	else
    	{
    		echo "response: ".$response.'<br/>';
    	}
    	//close channel
    	curl_close($ch);
    
    
    ?>

    Wednesday, October 09, 2013 10:35 AM
  • >what is Method id? How can i get it?

    Method Id is batch ID so you can start with 1 and increase by 1 if you want to add multiple records at a time. So your code should be like this:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:soap="http://schemas.microsoft.com/sharepoint/soap/">
      <soapenv:Header />
      <soapenv:Body>
        <soap:UpdateListItems>
          <soap:listName>Schedule</soap:listName>
          <soap:updates>
             <Batch OnError='Continue' ListVersion='1'>
                 <Method ID='1' Cmd='New'>
                     <Field Name="Title">New Test</Field>
                     <Field Name="EventDate">2013-10-09T12:00:00Z</Field>
                     <Field Name="EndDate">2013-10-09T01:00:00Z</Field>
                  </Method>
              </Batch>
          </soap:updates>
        </soap:UpdateListItems>
      </soapenv:Body>
    </soapenv:Envelope>
    

    http://msdn.microsoft.com/en-us/library/lists.lists.updatelistitems%28v=office.12%29.aspx


    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    Thursday, October 10, 2013 5:19 AM
    Moderator
  • Hi, 

    But i did't get any response from SharePoint, In Calendar i can't see any event?

    Thursday, October 10, 2013 10:38 AM
  • As i can see you are also passing user name and password in your code so verify that user permission in sharepoint site.  Make sure that user is having at least contribute right to add new item in calendar.

    Please verify that also let us know if you get any error


    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    Thursday, October 10, 2013 11:51 AM
    Moderator
  • I am not administrator. But i  checked permission in Site Action, In site action have no any checking option to check. 

    But i can create a content, and i created a Calendar. This permission is enough to add an item or event?

    Friday, October 11, 2013 6:57 AM
  • Yes, if you are able to create content in calendar list via UI then permission is fine. I have also tested below code in console application and working fine:

    DefaultService.Lists SPService = new DefaultService.Lists();
    SPService.Credentials = System.Net.CredentialCache.DefaultCredentials;
    System.Xml.XmlNode ndListView = SPService.GetListAndView("Test", "");
    string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value;
    string strViewID = ndListView.ChildNodes[1].Attributes["Name"].Value;
    
    XmlDocument doc = new XmlDocument();
    System.Xml.XmlElement batchs = doc.CreateElement("Batch");
    batchs.SetAttribute("OnError", "Continue");
    batchs.SetAttribute("ListVersion", "1");
    batchs.SetAttribute("ViewName", strViewID);
    
    batchs.InnerXml = "<Method ID='1' Cmd='New'><Field name='Title'>Addedfromservice</Field><Field Name='EventDate'>" + DateTime.Now + "</Field><Field Name='EndDate'>" + DateTime.Now.AddHours(1) + "</Field></Method>";
                try
                {
                    XmlNode ndReturn = SPService.UpdateListItems(strListID, batchs);
                    lblMsg.Text = ndReturn.OuterXml;
                }


    Hemendra:Yesterday is just a memory,Tomorrow we may never see
    Please remember to mark the replies as answers if they help and unmark them if they provide no help

    Friday, October 11, 2013 11:48 AM
    Moderator