Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, March 18, 2021

Create list item in SharePoint list using REST API

 HttpWebRequest contextinfoRequest =

    (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "/_api/contextinfo");

contextinfoRequest.Method = "POST";

contextinfoRequest.ContentType = "text/xml;charset=utf-8";

contextinfoRequest.ContentLength = 0;

contextinfoRequest.Headers.Add("Authorization", "Bearer " + accessToken);


HttpWebResponse contextinfoResponse = (HttpWebResponse)contextinfoRequest.GetResponse();

StreamReader contextinfoReader = new StreamReader(contextinfoResponse.GetResponseStream(), System.Text.Encoding.UTF8);

var formDigestXML = new XmlDocument();

formDigestXML.LoadXml(contextinfoReader.ReadToEnd());

var formDigestNode = formDigestXML.SelectSingleNode("//d:FormDigestValue", xmlnspm);

string formDigest = formDigestNode.InnerXml;


//Execute a REST request to get the list name and the entity type name for the list.

HttpWebRequest listRequest =

    (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "/_api/Web/lists(guid'" + listId + "')");

listRequest.Method = "GET";

listRequest.Accept = "application/atom+xml";

listRequest.ContentType = "application/atom+xml;type=entry";

listRequest.Headers.Add("Authorization", "Bearer " + accessToken);

HttpWebResponse listResponse = (HttpWebResponse)listRequest.GetResponse();

StreamReader listReader = new StreamReader(listResponse.GetResponseStream());

var listXml = new XmlDocument();

listXml.LoadXml(listReader.ReadToEnd());


//The entity type name is the required type when you construct a request to add a list item.

var entityTypeNode = listXml.SelectSingleNode("//atom:entry/atom:content/m:properties/d:ListItemEntityTypeFullName", xmlnspm);

var listNameNode = listXml.SelectSingleNode("//atom:entry/atom:content/m:properties/d:Title", xmlnspm);

string entityTypeName = entityTypeNode.InnerXml;

string listName = listNameNode.InnerXml;


//Execute a REST request to add an item to the list.

string itemPostBody = "{'__metadata':{'type':'" + entityTypeName + "'}, 'Title':'" + newItemName + "'}}";

Byte[] itemPostData = System.Text.Encoding.ASCII.GetBytes(itemPostBody);


HttpWebRequest itemRequest =

    (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "/_api/Web/lists(guid'" + listId + "')/Items");

itemRequest.Method = "POST";

itemRequest.ContentLength = itemPostBody.Length;

itemRequest.ContentType = "application/json;odata=verbose";

itemRequest.Accept = "application/json;odata=verbose";

itemRequest.Headers.Add("Authorization", "Bearer " + accessToken);

itemRequest.Headers.Add("X-RequestDigest", formDigest);

Stream itemRequestStream = itemRequest.GetRequestStream();


itemRequestStream.Write(itemPostData, 0, itemPostData.Length);

itemRequestStream.Close();


HttpWebResponse itemResponse = (HttpWebResponse)itemRequest.GetResponse();

RetrieveListItems(accessToken, listId);

Wednesday, March 2, 2016

How to retrieving next 7 Days Due Date Tasks from SharePoint List

C# Coding:


DateTime strdate = (DateTime)DateTime.Today.AddDays(7);
            string strgetDate = strdate.Year + "-" + strdate.Month + "-" + strdate.Date.Day;

SPWeb currentWeb = SPContext.Current.Web;
            SPList lst = currentWeb.Lists["Projects"];
            SPQuery sQuery = new SPQuery();
            sQuery.Query = "<Where><Geq><FieldRef Name='DueDate' /><Value Type='DateTime'>"+strgetDate+"</Value></Geq></Where><OrderBy><FieldRef Name='DueDate' /></OrderBy>";
            SPListItemCollection myColl = lst.GetItems(sQuery);
            if (myColl.Count > 0)
            {
                grdBind.DataSource = myColl.GetDataTable();
                grdBind.DataBind();
            }

How to retrieving Data from XML FileGrid view Using C# SharePoint

XML file inside:
<?xml version="1.0" encoding="utf-8" ?>
<Contacts>
  <Contact>
  <Fname>India</Fname>
  <City>Chennai</City>
  <Telno>99999999</Telno>
  </Contact>
  
  <Contact>
  <Fname>SW</Fname>
  <City>rome</City>
  <Telno>999999999</Telno>
    </Contact>


C# coding:
protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = getContacts();
        GridView1.DataBind();

      
    }
    public DataSet getContacts()
    {
        dSet = new DataSet();
        dSet.ReadXml(Server.MapPath("Contacts.xml"));
        return dSet;

    }

Wednesday, December 16, 2015

How to calculated data columns to DataTable c#


DataTable table = new DataTable("Purchase Items");

DataColumn workCol = table .Columns.Add("ID", typeof(Int32));
table .Columns.Add("UnitPrice", typeof(Double));
table .Columns.Add("Qty", typeof(Int32));
table .Columns.Add("Total", typeof(Double));
table .Columns.Add("Discount", typeof(Double));

table .Columns["Total"].Expression = "UnitPrice*Quantity";
table .Columns["Discount"].Expression = String.Format("Total*{0}",Discount);

grdData.DataSource = table ;
grdData.Update();

Saturday, April 25, 2015

How to load in current page in Asp.net and C#?

Page.Response.Redirect(Page.Request.RawUrl.ToString());