using System;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Configuration;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using System.Security;
using System.IO;
using System.Text;
using SpoWebApi.Models;
using System.Collections;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
namespace SpoWebApi.Controllers
{
public class SparksController : ApiController
{
[HttpGet]
[Route("api/hello")]
public HttpResponseMessage HelloWorld()
{
string result = "Hello world! Time is: " + DateTime.Now;
var resp = new HttpResponseMessage(HttpStatusCode.OK);
resp.Content = new StringContent(result, System.Text.Encoding.UTF8, "text/plain");
return resp;
}
[HttpGet]
[Route("api/UpdateSharePoint")]
public IHttpActionResult GetSparksDataUpdateSharePointList()
{
// this should process the order asynchronously
var tasks = new[]
{
Task.Run(() => processMethod())
};
return Ok("ok");
}
public void processMethod()
{
string sharePointUrl = ConfigurationManager.AppSettings["SharePointURL"];
var requestUrl = ConfigurationManager.AppSettings["getResourceUri"];
var _sparkVMSKey = ConfigurationManager.AppSettings["vmsAppKey"];
var _sparkServiceReqKey = ConfigurationManager.AppSettings["serviceReqResKey"];
//var _sparksrcAppKey = ConfigurationManager.AppSettings["srcAppKey"];
string _spUserName = ConfigurationManager.AppSettings["userName"];
string _spPwd = ConfigurationManager.AppSettings["Password"];
string _spListName = ConfigurationManager.AppSettings["listName"];
Stream dataStream;
WebResponse response;
string responseFromServer;
StreamReader reader;
responseFromServer = "";
reader = null;
dataStream = null;
response = null;
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var _ErrorLogPath = "/TestMapPath";
var returnMessage = "";
ArrayList myAL = new ArrayList();
string postData = string.Format("destAppKey=" + _sparkVMSKey + "&resKey=" + _sparkServiceReqKey);//required for sendTO + "&srcAppKey=" + _sparksrcAppKey
try
{
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => { return true; };
WebRequest request = WebRequest.Create(requestUrl);
request.Method = "POST";
// string postData = dataPost;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
response = request.GetResponse();
dataStream = response.GetResponseStream();
reader = new StreamReader(dataStream);
responseFromServer = reader.ReadToEnd();
Result sparksResult = Newtonsoft.Json.JsonConvert.DeserializeObject<Result>(responseFromServer);
string webTitle = "";
if (sparksResult.sparksResponse.status)
{
foreach (Datum outData in sparksResult.sparksResponse.data)
{
Resdata sparksResData = Newtonsoft.Json.JsonConvert.DeserializeObject<Resdata>(outData.resdata);
//if status is true , please update SP list - 3 columns , Update list item depends on ProcessType
//VisitorRequestID ( CabRequestRaised , ITRequestRaised , FacilitiesReqRaised ) either YES OR NO
string spItemID = sparksResData.VisitorRequestID;
if (sparksResData.status == "true" && sparksResData.processType.Contains("AdhocCab"))
{
try
{
using (ClientContext clientContext = new ClientContext(sharePointUrl))
{
var passWord = new SecureString();
foreach (char c in _spPwd.ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(_spUserName, passWord);
Web web = clientContext.Web;
List oList = clientContext.Web.Lists.GetByTitle(_spListName);
ListItem oListItem = oList.GetItemById(spItemID);// spItemID);
oListItem["CabRequestRaised"] = "1";
oListItem["VisitorRequestID"] = sparksResData.requestId;
oListItem["SSMsg"] = sparksResData.message;
oListItem["PendingWith"] = sparksResData.pendingWith;
oListItem.Update();
clientContext.ExecuteQuery();
clientContext.Load(web);
clientContext.ExecuteQuery();
webTitle = web.Title;
myAL.Add(spItemID);
}
}
catch (Exception ex)
{
var errmsg = ex.Message;
}
returnMessage += sparksResData.message + webTitle;
}
else if (sparksResData.status == "true" && sparksResData.processType.Contains("ITSupport"))
{
//update ITRequestRaised;
try
{
using (ClientContext clientContext = new ClientContext(sharePointUrl))
{
var passWord = new SecureString();
foreach (char c in _spPwd.ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(_spUserName, passWord);
Web web = clientContext.Web;
List oList = clientContext.Web.Lists.GetByTitle(_spListName);
ListItem oListItem = oList.GetItemById(spItemID);
oListItem["ITRequestRaised"] = "1";
oListItem["VisitorRequestID"] = sparksResData.requestId;
oListItem["SSMsg"] = sparksResData.message;
oListItem["PendingWith"] = sparksResData.pendingWith;
oListItem.Update();
clientContext.ExecuteQuery();
myAL.Add(spItemID);
}
}
catch (Exception ex)
{
var errmsg = ex.Message;
}
}
else if (sparksResData.status == "true" && sparksResData.processType.Contains("Facilities"))
{
//update FacilitiesReqRaised
try
{
using (ClientContext clientContext = new ClientContext(sharePointUrl))
{
var passWord = new SecureString();
foreach (char c in _spPwd.ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(_spUserName, passWord);
Web web = clientContext.Web;
List oList = clientContext.Web.Lists.GetByTitle(_spListName);
ListItem oListItem = oList.GetItemById(spItemID);
oListItem["FacilitiesReqRaised"] = "1";
oListItem["VisitorRequestID"] = sparksResData.requestId;
oListItem["SSMsg"] = sparksResData.message;
oListItem["PendingWith"] = sparksResData.pendingWith;
oListItem.Update();
clientContext.ExecuteQuery();
myAL.Add(spItemID);
}
}
catch (Exception ex)
{
var errmsg = ex.Message;
}
}
}
}
}
catch (Exception e)
{
var error = e.InnerException;
ErrorLog errorLog = new ErrorLog();
errorLog.message = "Something Went Wrong";
errorLog.status = false;
returnMessage = JsonConvert.SerializeObject(errorLog);
System.IO.File.AppendAllText(System.Web.HttpContext.Current.Server.MapPath(_ErrorLogPath), e.Message + Environment.NewLine);
}
try
{
//ArrayList myDeleteIds = new ArrayList();
//myDeleteIds.Add("12346781_Facilities");
if (myAL.Count > 0)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
string output = jss.Serialize(myAL);
string postData1 = string.Format("destAppKey=" + _sparkVMSKey + "&resKey=" + _sparkServiceReqKey + "&dataIds=" + output);
string requestDeleteUrl = ConfigurationManager.AppSettings["deleteReports"];
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => { return true; };
WebRequest request = WebRequest.Create(requestDeleteUrl);
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(postData1);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
response = request.GetResponse();
dataStream = response.GetResponseStream();
reader = new StreamReader(dataStream);
responseFromServer = reader.ReadToEnd();
}
}
catch (Exception ex)
{
var errmsg = ex.Message;
}
}
}
}
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Configuration;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using System.Security;
using System.IO;
using System.Text;
using SpoWebApi.Models;
using System.Collections;
using Newtonsoft.Json;
using System.Web.Script.Serialization;
namespace SpoWebApi.Controllers
{
public class SparksController : ApiController
{
[HttpGet]
[Route("api/hello")]
public HttpResponseMessage HelloWorld()
{
string result = "Hello world! Time is: " + DateTime.Now;
var resp = new HttpResponseMessage(HttpStatusCode.OK);
resp.Content = new StringContent(result, System.Text.Encoding.UTF8, "text/plain");
return resp;
}
[HttpGet]
[Route("api/UpdateSharePoint")]
public IHttpActionResult GetSparksDataUpdateSharePointList()
{
// this should process the order asynchronously
var tasks = new[]
{
Task.Run(() => processMethod())
};
return Ok("ok");
}
public void processMethod()
{
string sharePointUrl = ConfigurationManager.AppSettings["SharePointURL"];
var requestUrl = ConfigurationManager.AppSettings["getResourceUri"];
var _sparkVMSKey = ConfigurationManager.AppSettings["vmsAppKey"];
var _sparkServiceReqKey = ConfigurationManager.AppSettings["serviceReqResKey"];
//var _sparksrcAppKey = ConfigurationManager.AppSettings["srcAppKey"];
string _spUserName = ConfigurationManager.AppSettings["userName"];
string _spPwd = ConfigurationManager.AppSettings["Password"];
string _spListName = ConfigurationManager.AppSettings["listName"];
Stream dataStream;
WebResponse response;
string responseFromServer;
StreamReader reader;
responseFromServer = "";
reader = null;
dataStream = null;
response = null;
var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
var _ErrorLogPath = "/TestMapPath";
var returnMessage = "";
ArrayList myAL = new ArrayList();
string postData = string.Format("destAppKey=" + _sparkVMSKey + "&resKey=" + _sparkServiceReqKey);//required for sendTO + "&srcAppKey=" + _sparksrcAppKey
try
{
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => { return true; };
WebRequest request = WebRequest.Create(requestUrl);
request.Method = "POST";
// string postData = dataPost;
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
response = request.GetResponse();
dataStream = response.GetResponseStream();
reader = new StreamReader(dataStream);
responseFromServer = reader.ReadToEnd();
Result sparksResult = Newtonsoft.Json.JsonConvert.DeserializeObject<Result>(responseFromServer);
string webTitle = "";
if (sparksResult.sparksResponse.status)
{
foreach (Datum outData in sparksResult.sparksResponse.data)
{
Resdata sparksResData = Newtonsoft.Json.JsonConvert.DeserializeObject<Resdata>(outData.resdata);
//if status is true , please update SP list - 3 columns , Update list item depends on ProcessType
//VisitorRequestID ( CabRequestRaised , ITRequestRaised , FacilitiesReqRaised ) either YES OR NO
string spItemID = sparksResData.VisitorRequestID;
if (sparksResData.status == "true" && sparksResData.processType.Contains("AdhocCab"))
{
try
{
using (ClientContext clientContext = new ClientContext(sharePointUrl))
{
var passWord = new SecureString();
foreach (char c in _spPwd.ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(_spUserName, passWord);
Web web = clientContext.Web;
List oList = clientContext.Web.Lists.GetByTitle(_spListName);
ListItem oListItem = oList.GetItemById(spItemID);// spItemID);
oListItem["CabRequestRaised"] = "1";
oListItem["VisitorRequestID"] = sparksResData.requestId;
oListItem["SSMsg"] = sparksResData.message;
oListItem["PendingWith"] = sparksResData.pendingWith;
oListItem.Update();
clientContext.ExecuteQuery();
clientContext.Load(web);
clientContext.ExecuteQuery();
webTitle = web.Title;
myAL.Add(spItemID);
}
}
catch (Exception ex)
{
var errmsg = ex.Message;
}
returnMessage += sparksResData.message + webTitle;
}
else if (sparksResData.status == "true" && sparksResData.processType.Contains("ITSupport"))
{
//update ITRequestRaised;
try
{
using (ClientContext clientContext = new ClientContext(sharePointUrl))
{
var passWord = new SecureString();
foreach (char c in _spPwd.ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(_spUserName, passWord);
Web web = clientContext.Web;
List oList = clientContext.Web.Lists.GetByTitle(_spListName);
ListItem oListItem = oList.GetItemById(spItemID);
oListItem["ITRequestRaised"] = "1";
oListItem["VisitorRequestID"] = sparksResData.requestId;
oListItem["SSMsg"] = sparksResData.message;
oListItem["PendingWith"] = sparksResData.pendingWith;
oListItem.Update();
clientContext.ExecuteQuery();
myAL.Add(spItemID);
}
}
catch (Exception ex)
{
var errmsg = ex.Message;
}
}
else if (sparksResData.status == "true" && sparksResData.processType.Contains("Facilities"))
{
//update FacilitiesReqRaised
try
{
using (ClientContext clientContext = new ClientContext(sharePointUrl))
{
var passWord = new SecureString();
foreach (char c in _spPwd.ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials(_spUserName, passWord);
Web web = clientContext.Web;
List oList = clientContext.Web.Lists.GetByTitle(_spListName);
ListItem oListItem = oList.GetItemById(spItemID);
oListItem["FacilitiesReqRaised"] = "1";
oListItem["VisitorRequestID"] = sparksResData.requestId;
oListItem["SSMsg"] = sparksResData.message;
oListItem["PendingWith"] = sparksResData.pendingWith;
oListItem.Update();
clientContext.ExecuteQuery();
myAL.Add(spItemID);
}
}
catch (Exception ex)
{
var errmsg = ex.Message;
}
}
}
}
}
catch (Exception e)
{
var error = e.InnerException;
ErrorLog errorLog = new ErrorLog();
errorLog.message = "Something Went Wrong";
errorLog.status = false;
returnMessage = JsonConvert.SerializeObject(errorLog);
System.IO.File.AppendAllText(System.Web.HttpContext.Current.Server.MapPath(_ErrorLogPath), e.Message + Environment.NewLine);
}
try
{
//ArrayList myDeleteIds = new ArrayList();
//myDeleteIds.Add("12346781_Facilities");
if (myAL.Count > 0)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
string output = jss.Serialize(myAL);
string postData1 = string.Format("destAppKey=" + _sparkVMSKey + "&resKey=" + _sparkServiceReqKey + "&dataIds=" + output);
string requestDeleteUrl = ConfigurationManager.AppSettings["deleteReports"];
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => { return true; };
WebRequest request = WebRequest.Create(requestDeleteUrl);
request.Method = "POST";
byte[] byteArray = Encoding.UTF8.GetBytes(postData1);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
response = request.GetResponse();
dataStream = response.GetResponseStream();
reader = new StreamReader(dataStream);
responseFromServer = reader.ReadToEnd();
}
}
catch (Exception ex)
{
var errmsg = ex.Message;
}
}
}
}
No comments:
Post a Comment