Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.
Showing posts with label SharePoint Default WebServices. Show all posts
Showing posts with label SharePoint Default WebServices. Show all posts

Monday, November 28, 2011

Delete a record based on ItemID 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 PrasadBolla3;
using System.Xml;
public partial class Default9 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnDelete_Click(object sender, EventArgs e)
{
string strDeleteCommand = string.Empty;
strDeleteCommand = ""+ddlItemID.SelectedItem.Value+"";
PrasadBolla3.Lists objLogin = new PrasadBolla3.Lists();
objLogin.Credentials = new System.Net.NetworkCredential("true_indian9000", "prasad1234", "apptix");
objLogin.Url = "http://prasad.bolla3.sharepointsite.net/_vti_bin/Lists.asmx";
string strListName = "Login";
XmlDocument xmlDoc = new System.Xml.XmlDocument();

System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");

elBatch.SetAttribute("OnError", "Continue");

elBatch.SetAttribute("ListVersion", "1");

elBatch.InnerXml = strDeleteCommand;

XmlNode ndReturn = objLogin.UpdateListItems(strListName, elBatch);

string result = ndReturn.OuterXml.ToString();
Response.Write("Record Deleted");
}
}

Updating a Record basedon ItemID 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 PrasadBolla3;
using System.Xml;

public partial class Default8 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnUpdate_Click(object sender, EventArgs e)
{
string strUpdateCommand = string.Empty;
strUpdateCommand = "" + ddlItemID.SelectedItem.Value +"" + txtUserName.Text + "" + txtPassWord.Text + "";
PrasadBolla3.Lists objLogin = new PrasadBolla3.Lists();
objLogin.Credentials = new System.Net.NetworkCredential("true_indian9000", "prasad1234", "apptix");
objLogin.Url = "http://prasad.bolla3.sharepointsite.net/_vti_bin/Lists.asmx";
string strListName = "Login";
XmlDocument xmlDoc = new System.Xml.XmlDocument();

System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");

elBatch.SetAttribute("OnError", "Continue");

elBatch.SetAttribute("ListVersion", "1");

elBatch.InnerXml = strUpdateCommand;

XmlNode ndReturn = objLogin.UpdateListItems(strListName, elBatch);

string result = ndReturn.OuterXml.ToString();
txtUserName.Text = "";
txtPassWord.Text = "";
txtUserName.Focus();
}
}

Binding Data From SharePoint List to Gridview 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 Prasad;
using System.Xml;

public partial class Default6 : System.Web.UI.Page
{
DataSet dset = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{
Prasad.Lists listService = new Prasad.Lists();
listService.UseDefaultCredentials=true;
listService.Url = "http://vlnrm:100/_vti_bin/Lists.asmx";
//string strListName = "{296A9F52-5A48-4CB9-9294-47F93DF2F85F}";
string strListName = "Tasks";
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement query = xmlDoc.CreateElement("Query");

System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
viewFields.InnerXml = "<fieldref name="\&quot;ID\&quot;"><fieldref name="\&quot;Title\&quot;"><fieldref name="\&quot;Status\&quot;"><fieldref name="\&quot;Priority\&quot;"><fieldref name="\&quot;AssignedTo\&quot;">";

query.InnerXml = "<orderby><fieldref ascending="False" name="ID"></fieldref></orderby>";
System.Xml.XmlNode nodeListItems = listService.GetListItems(strListName, null, query, viewFields, "5", null, null);
//System.Xml.XmlNode nodeListItems = listService.GetListItems(listName, viewName, query, viewFields, rowLimit, queryOptions, null);
DataSet ds = getData(nodeListItems);
DataTable dt = ds.Tables["row"];
GridView1.DataSource = dt;
GridView1.DataBind();
System.Xml.XmlElement batchElement = xmlDoc.CreateElement("Batch");
batchElement.SetAttribute("OnError", "Continue");
batchElement.SetAttribute("ListVersion", "1");
Response.Write("");          for (int i = 0; i &lt; dt.Rows.Count; i++)         
{             
string strID = dt.Rows[i]["ows_ID"].ToString();             
string strTitle = dt.Rows[i]["ows_Title"].ToString();            
 string strStatus = dt.Rows[i]["ows_Status"].ToString();             
string strPriority = dt.Rows[i]["ows_Priority"].ToString();             

string strAssignedTo = dt.Rows[i]["ows_AssignedTo"].ToString();             
strAssignedTo = strAssignedTo.Substring(strAssignedTo.IndexOf('#')+1);             
Response.Write("");             
Response.Write("");             
Response.Write("");             
if (strStatus == "Not Started")             
{                 
Response.Write("");
Response.Write("");
Response.Write("");
Response.Write("
");
Response.Write("
");
Response.Write("");
Response.Write("
");

}
Response.Write("</fieldref></fieldref></fieldref></fieldref></fieldref>
<table><tbody>
<tr><td>");
Response.Write(strID);
Response.Write("</td><td>");
Response.Write(strTitle);
Response.Write("</td><td bgcolor="Red">");

}
else if (strStatus == "Completed")
{
Response.Write("</td><td bgcolor="Green">");
}
else if (strStatus == "In Progress")
{
Response.Write("</td><td bgcolor="Orange">");
}
else if (strStatus == "Deferred")
{
Response.Write("</td><td bgcolor="Pink">");
}
Response.Write("<b>");
Response.Write("<span style="color: white;">");
Response.Write(strStatus);
Response.Write("</span>");
Response.Write("</b>");
Response.Write("</td><td>");
Response.Write(strPriority);
Response.Write("</td><td>");
Response.Write(strAssignedTo);
Response.Write("</td></tr>
<tr><td bgcolor="blue" colspan="5">");
Response.Write("</td></tr>
</tbody></table>
");
try
{
//listService.UpdateListItems(listName, batchElement);
//XmlNode strNode = listService.UpdateListItems(strListName, batchElement);
//Response.Write("Response " + strNode.OuterXml);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
Response.Write("Message:" + ex.Message);
}
}
public DataSet getData(XmlNode xmlnodeinput)
{
if (xmlnodeinput != null)
{
XmlTextReader xtr = new XmlTextReader(xmlnodeinput.OuterXml, XmlNodeType.Element, null);
//dataset = new DataSet();
dset.ReadXml(xtr);
}
return dset;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[3].Text = e.Row.Cells[3].Text.Substring(e.Row.Cells[3].Text.IndexOf('#') + 1);
}
}

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://vlnrm:100/Sample/Lists/MyList/TestFolder2/" + DropDownList1.SelectedItem.Value;
strInsertCommand = "" + txtTitle.Text + "";
MySite_Dat.Lists objLogin = new MySite_Dat.Lists();
objLogin.UseDefaultCredentials=true;
objLogin.Url = "http://http://vlnrm:100/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 = "";
}
}

Creating a SubFolder inside a list using SharePoint Default WebServices

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 MyService;
using System.Xml;
public partial class Default16 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
MyService.Lists listProxy = new MyService.Lists();

//string xmlconst = "0http://vlnrm:100/Sample/Lists/MyList/TestFolder2/Test001/"+txtCreateSubFolder.Text+"";
string str = "TestFolder2/" + txtCreateSubFolder.Text;
if (str.ToString().Contains("&"))
{

//str = str.Replace("&", "/g");

}
string xmlconst = "1" + str + "";




listProxy.Url = "http://vlnrm:100/Sample/_vti_bin/lists.asmx";
listProxy.UseDefaultCredentials = true;


string strListName = "MyList";
XmlDocument xmlDoc = new System.Xml.XmlDocument();

System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");

elBatch.SetAttribute("OnError", "Continue");

elBatch.SetAttribute("ListVersion", "1");


elBatch.InnerXml = xmlconst;

XmlNode ndReturn = listProxy.UpdateListItems(strListName, elBatch);

string result = ndReturn.OuterXml.ToString();
Page.RegisterClientScriptBlock("alert", "");
txtCreateSubFolder.Text = "";
}
}

Retrieving subsites under the topsite to a spgridview using SharePoint Default WebServices

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 Webs;
using System.Xml;
public partial class Default11 : System.Web.UI.Page
{
DataSet dset = new DataSet();
//DataSet dset1 = new DataSet();
//DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Prasad.Lists listService = new Prasad.Lists();
Webs.Webs web_Service = new Webs.Webs();
web_Service.UseDefaultCredentials=true;
web_Service.Url = "http://vlnrm:100/_vti_bin/webs.asmx";
System.Xml.XmlNode nodeListItems = web_Service.GetWebCollection();
DataSet ds = getData(nodeListItems);
DataTable dt = ds.Tables["Web"];
GridView1.DataSource = dt;
GridView1.DataBind();
Response.Write(""); Response.Write(""); Response.Write(""); Response.Write(""); for (int i = 0; i < dt.Rows.Count; i++) { string strTitle = dt.Rows[i]["Title"].ToString(); string strUrl = dt.Rows[i]["Url"].ToString(); Response.Write(""); Response.Write(""); Response.Write(""); } Response.Write("
");
Response.Write("Subsites List");
Response.Write("
");
Response.Write(""+strTitle+"");
Response.Write("
");
}
}
public DataSet getData(XmlNode xmlnodeinput)
{
if (xmlnodeinput != null)
{
XmlTextReader xtr = new XmlTextReader(xmlnodeinput.OuterXml, XmlNodeType.Element, null);
//dataset = new DataSet();
dset.ReadXml(xtr);
}
return dset;
}
/*public DataSet getData1(XmlNode xmlnodeinput)
{
if (xmlnodeinput != null)
{
XmlTextReader xtr = new XmlTextReader(xmlnodeinput.OuterXml, XmlNodeType.Element, null);
//dataset = new DataSet();
dset1.ReadXml(xtr);
}
return dset1;
}*/

}

Binding Events to calendar based on Calendar Date

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 Prasad;
using System.Xml;
public partial class Default10 : System.Web.UI.Page
{
DataSet dset = new DataSet();
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{


DateTime dtNow= Calendar1.SelectedDate;
string toDayFormat = dtNow.ToString("u");
string[] EventStartDate = toDayFormat.Split(new Char[] { ' ' });


Prasad.Lists listService = new Prasad.Lists();
listService.UseDefaultCredentials=true;
listService.Url = "http://vlnrm:100/_vti_bin/Lists.asmx";
string strListName = "Events";
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement query = xmlDoc.CreateElement("Query");

System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
viewFields.InnerXml = "";
//string strDate=Calendar1.s
//2010-08-10T00:00:00Z
//8/11/2010 12:00:00 AM
query.InnerXml = "" + EventStartDate[0] + "";
System.Xml.XmlNode nodeListItems = listService.GetListItems(strListName, null, query, viewFields, null, null, null);
//System.Xml.XmlNode nodeListItems = listService.GetListItems(listName, viewName, query, viewFields, rowLimit, queryOptions, null);
DataSet ds = getData(nodeListItems);
DataTable dt = ds.Tables["row"];
GridView1.DataSource = dt;
GridView1.DataBind();
System.Xml.XmlElement batchElement = xmlDoc.CreateElement("Batch");
batchElement.SetAttribute("OnError", "Continue");
batchElement.SetAttribute("ListVersion", "1");
}
public DataSet getData(XmlNode xmlnodeinput)
{
if (xmlnodeinput != null)
{
XmlTextReader xtr = new XmlTextReader(xmlnodeinput.OuterXml, XmlNodeType.Element, null);
//dataset = new DataSet();
dset.ReadXml(xtr);
}
return dset;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[2].Text = Convert.ToDateTime(e.Row.Cells[2].Text).ToShortDateString();
}
}
}

Uploading a document to a document library using SharePoint Default WebServices

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.Net;
using System.IO;
using System.Xml;
using MySite;
using System.Text;
public partial class Add_New_Document : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnUpload_Click(object sender, EventArgs e)
{



//Create a filestream containing the document you want to upload

FileStream oSourceFile = File.OpenRead(fuBrowse.PostedFile.FileName);

//Create a webrequest pointing to the site and document library you want to

//upload the document to. In addition specify the document name
string strOnlyFile = fuBrowse.PostedFile.FileName;

//onlyFile=onlyFile.Substring
strOnlyFile = strOnlyFile.Substring((fuBrowse.PostedFile.FileName).LastIndexOf(@"\") + 1);
string strCompleteFile="http://vlnrm:100/UserPages/"+strOnlyFile;
WebRequest oWr = WebRequest.Create(strCompleteFile);

oWr.Method = "PUT";

oWr.Headers.Add("Overwrite", "F");

//oWr.Timeout = Timeout.Infinite;

oWr.Credentials = CredentialCache.DefaultCredentials;



//Literally copy the bytes of the filestream into the request stream to "upload" the document to the library

byte[] oBytes = new byte[1024];

using (Stream oRq = oWr.GetRequestStream())

{

for (; ; )

{

int iBytes = oSourceFile.Read(oBytes, 0, oBytes.Length);

if (iBytes <= 0) break; oRq.Write(oBytes, 0, iBytes); } oSourceFile.Close(); } WebResponse oResponse = oWr.GetResponse(); //Now create the XML to submit to the list service web service StringBuilder oSb = new StringBuilder(); oSb.Append("");

oSb.Append(" ");

oSb.Append(" "+strCompleteFile+"");

//Change the title of the document - replace or add to the document properties as necessary

oSb.Append(" New Title");

oSb.Append("
");



XmlDocument oDoc = new System.Xml.XmlDocument();

XmlElement oXBatch = oDoc.CreateElement("Batch");

oXBatch.SetAttribute("OnError", "Continue");

oXBatch.SetAttribute("PreCalc", "TRUE");

oXBatch.SetAttribute("ListVersion", "0");

oXBatch.InnerXml = oSb.ToString();



//Do the update replace the ListWebService object with your web service object

MySite.Lists oLstSvc = new MySite.Lists();

oLstSvc.Credentials = CredentialCache.DefaultNetworkCredentials;

XmlNode oRet = oLstSvc.UpdateListItems("UserPages", oXBatch);
Page.RegisterClientScriptBlock("alert", "");

}
}

Binding data to Asp.Net GridView using SharePoint Default WebServices

Insertion Using SharePoint Default Webservices

string strInsertCommand = string.Empty;
strInsertCommand = "" + txtUserName.Text + "" + txtPassword.Text + "";
InsertSampleService.Lists objLogin = new InsertSampleService.Lists();
objLogin.UseDefaultCredentials=true;
objLogin.Url = "http://vlnrm:100/_vti_bin/Lists.asmx";
string strListName = "Test";
XmlDocument xmlDoc = new System.Xml.XmlDocument();

System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");

elBatch.SetAttribute("OnError", "Continue");

elBatch.SetAttribute("ListVersion", "1");

elBatch.InnerXml = strInsertCommand;

XmlNode ndReturn = objLogin.UpdateListItems(strListName, elBatch);

string result = ndReturn.OuterXml.ToString();
txtUserName.Text = "";
txtPassword.Text = "";
txtUserName.Focus();