Tuesday, January 3, 2012

Inserting a listitem inside a subfolder in a sharepoint list using SharePoint Default Webservice.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using MySite_Dat;
using Microsoft.SharePoint;
public partial class Default17 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string strInsertCommand = string.Empty;
        string strSelectedFolder = string.Empty;
        string strListName = "MyList";
        strSelectedFolder = "http://agmsm:9999/sites/Venkat/Sample/Lists/MyList/TestFolder2/" + DropDownList1.SelectedItem.Value;
        strInsertCommand = "<Method ID='1' Cmd='New'><Field Name='Title'>" + txtTitle.Text + "</Field></Method>";
        MySite_Dat.Lists objLogin = new MySite_Dat.Lists();
        objLogin.UseDefaultCredentials=true;
        objLogin.Url = "http://agmsm:9999/sites/Venkat/Sample/_vti_bin/Lists.asmx";
        XmlDocument xmlDoc = new System.Xml.XmlDocument();
        System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");
        elBatch.SetAttribute("RootFolder", strSelectedFolder);
        elBatch.SetAttribute("OnError", "Continue");
        elBatch.SetAttribute("ListVersion", "1");
        elBatch.InnerXml = strInsertCommand;
        try
        {
            XmlNode ndReturn = objLogin.UpdateListItems(strListName, elBatch);
            string result = ndReturn.OuterXml.ToString();
        }
        catch (Exception ex)
        {
            this.Controls.Add(new LiteralControl(ex.ToString()));
        }
        txtTitle.Text = "";
    }
}

No comments:

Post a Comment