Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Web Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Twisting My Arm - How I was Persuaded to Change StructureMap VersionsHas FireFox lost its Mojo, are FireFox user jumping ship to Chrome? »
    comments

    We recently had a requirement to programatically create an entry in an existing Sharepoint calendar, so here is an example of how we went about doing this.

    I'll be using Visual Studio 2008 for this, but the process will be similar for any other versions you are using. First of all, you will need to add a reference to one of the Sharepoint web references, so right-click your project and select Add Web Reference. You'll then be presented with a screen that asks you for a URL for that web service, so go ahead and write it in the following format:

    http://nis/_vti_bin/lists.asmx

    NOTE: You'll need to replace the "nis" section with the path to your own Sharepoint website.

    The following screen will then be presented displaying the list of available methods:

    Change the Web Reference Name to "Sharepoint.Lists" and click the "Add Reference" button to add it to your project.

    Next, you'll need to open up a new form/page/module (whichever is appropriate to your solution), add a few imports:

    1. Imports Sharepoint
    2. Imports System.Text
    3. Imports System.XML

    and then we'll create a function that builds an XML string ready to be passed to the Sharepoint web service:

    1. Public Function CreateCalendarEntry(ByVal CalendarName As String, ByVal Title As String, ByVal Description As String, ByVal AddToDate As DateTime, ByVal FullDay As Boolean, ByVal LengthInMinutes As Double) As XmlNode
    2.  
    3.         ' Declarations
    4.         Dim sBatch As New StringBuilder
    5.  
    6.         ' Get a reference to the list web service
    7.         Dim listService As New Lists
    8.         listService.Credentials = System.Net.CredentialCache.DefaultCredentials
    9.  
    10.         ' Create the XML to be passed to the calendar
    11.         sBatch.Append("<Method ID='1' Cmd='New'>")
    12.         sBatch.Append("<Field Name='Title'>" & Title & "</Field>")
    13.         If FullDay = True Then
    14.             sBatch.Append("<Field Name='EventDate'>" & AddToDate.ToString("yyyy-MM-dd") & "</Field>")
    15.             sBatch.Append("<Field Name='EndDate'>" & AddToDate.ToString("yyyy-MM-dd") & "</Field>")
    16.             sBatch.Append("<Field Name='fAllDayEvent'>1</Field>")
    17.         Else
    18.             sBatch.Append("<Field Name='EventDate'>" & AddToDate.ToString("yyyy-MM-ddTHH:mm:ssZ") & "</Field>")
    19.             sBatch.Append("<Field Name='EndDate'>" & AddToDate.AddMinutes(LengthInMinutes).ToString("yyyy-MM-ddTHH:mm:ssZ") & "</Field>")
    20.             sBatch.Append("<Field Name='fAllDayEvent'>0</Field>")
    21.         End If
    22.         sBatch.Append("<Field Name='Description'>" & Description & "</Field>")
    23.         sBatch.Append("</Method>")
    24.  
    25.         ' Add the calendar XML to a batch
    26.         Dim xmlDoc2 As New System.Xml.XmlDocument()
    27.         Dim Batch As System.Xml.XmlElement = xmlDoc2.CreateElement("Batch")
    28.         Batch.InnerXml = sBatch.ToString
    29.  
    30.         ' Pass the XML to the webservice and return the result
    31.         Return listService.UpdateListItems(CalendarName, Batch)
    32.  
    33.     End Function

    As you can see from the above, we build an XML string based upon:

    1. The name of the calendar
    2. A title
    3. A description
    4. When it needs to be entered into the calendar (i.e on a certain date, for a full day or a certain amount of hours)

    We then pass the XML to the web service which will then insert the calendar entry and return a resulting XML string for you to check it's success.

    So, as an example call to the service, if you pass in some test data:

    1. MyTestPage.CreateCalendarEntry("Development Team Calendar", "LessThanDot.com", "This is a sample calendar entry", System.DateTime.Now, False, 30)

    then you should see an example entry in your calendar:

    along with some further details once you click into the actual entry:

    About the Author

    User bio imageMark is primarily an ASP.NET developer and also has skills in SQL Server, HTML and CSS. He is based in the North East of England and has a 2 year old son named George. Mark is the founder of http://aspnetlibrary.com, has a blog at http://weblogs.asp.net/marksmith and is the director of http://mdssolutions.co.uk.
    Social SitingsTwitterHomePageLTD RSS Feed
    4435 views
    InstapaperVote on HN

    2 comments

    Comment from: Krosty [Visitor]
    Krosty How can I add an attachment.

    Thx
    11/08/11 @ 14:19
    Comment from: clem [Visitor]
    clem If the list has SP Designer or other worklows associated with it, will they be executed when an item is added?
    10/15/12 @ 08:03

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)