Error using Connect to receive event notification - c#

I am just starting with the DocuSign API and Connect and have published a Webhook to receive event notifications. However, I am getting a 404 not found error in my Connect Failures list in the admin portal. This is the uri of my API method:
http://documentsigningapi.networxsolutions.co.uk/Webhook/DocumentSigned
I have set this in the URL to publish option in the configuration settings.
Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Xml;
namespace DocumentSigningAPI.Controllers
{
public class WebhookController : ApiController
{
public void DocumentSigned(HttpRequestMessage request)
{
var xmldoc = new XmlDocument();
var result = request.Content.ReadAsStreamAsync().Result;
xmldoc.Load(result);
var mgr = new XmlNamespaceManager(xmldoc.NameTable);
mgr.AddNamespace("a", "http://www.docusign.net/API/3.0");
XmlNode envelopeStatus = xmldoc.SelectSingleNode("//a:EnvelopeStatus", mgr);
XmlNode envelopeId = envelopeStatus.SelectSingleNode("//a:EnvelopeID", mgr);
XmlNode status = envelopeStatus.SelectSingleNode("//a:Status", mgr);
if (envelopeId != null)
{
System.IO.File.WriteAllText("C:/inetpub/DocumentSigningDemo/Documents/" +
envelopeId.InnerText + "_" + status.InnerText + "_" + Guid.NewGuid() + ".xml", xmldoc.OuterXml);
}
if (status.InnerText == "Completed")
{
// Loop through the DocumentPDFs element, storing each document.
XmlNode docs = xmldoc.SelectSingleNode("//a:DocumentPDFs", mgr);
foreach (XmlNode doc in docs.ChildNodes)
{
string documentName = doc.ChildNodes[0].InnerText; // pdf.SelectSingleNode("//a:Name", mgr).InnerText;
string documentId = doc.ChildNodes[2].InnerText; // pdf.SelectSingleNode("//a:DocumentID", mgr).InnerText;
string byteStr = doc.ChildNodes[1].InnerText; // pdf.SelectSingleNode("//a:PDFBytes", mgr).InnerText;
System.IO.File.WriteAllText("C:/inetpub/DocumentSigningDemo/Documents/" + envelopeId.InnerText + "_" + documentId + "_" + documentName, byteStr);
}
}
}
}
}

Seems that the API controller was not found, so I have now put my function into a web app instead of an API, which is now working.

Related

Get Innertext from an XML string which has only one node without looping

I have an XML string which looks like below
<?xml version="1.0" encoding="Windows-1252"?><Product><ID>0701161476416</ID><UNIQUE_ID>test26051602</UNIQUE_ID><STATUS>DONE</STATUS></Product>
It is know that my XML string will always has a single node ans so I do not want to look, instead I would like to get Unique_ID and Status inner values without looping.
May I know a better way to do it and I do have the below code which actually loops through each node
XmlDocument xm = new XmlDocument();
xm.LoadXml(XML_STRING);
XmlNodeList xnList = xm.SelectNodes("/Product/Product");
foreach (XmlNode xn in xnList)
{
string uniqueID = xn["UNIQUE_ID"].InnerText;
string status = xn["STATUS"].InnerText;
}
There is SelectSingleNode() which you can use for this purpose :
XmlNode product = xm.SelectSingleNode("/Product/Product");
string uniqueID = product["UNIQUE_ID"].InnerText;
string status = product["STATUS"].InnerText;
Or, if Product is the root element, then you can access it from DocumentElement property of the XmlDocument :
XmlNode product = xm.DocumentElement;
string uniqueID = product["UNIQUE_ID"].InnerText;
string status = product["STATUS"].InnerText;
If you have more than one product try xml linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
"<?xml version=\"1.0\" encoding=\"Windows-1252\"?>" +
"<Products>" +
"<Product>" +
"<ID>0701161476416</ID>" +
"<UNIQUE_ID>test26051603</UNIQUE_ID>" +
"<STATUS>DONE</STATUS>" +
"</Product>" +
"<Product>" +
"<ID>0701161476417</ID>" +
"<UNIQUE_ID>test26051604</UNIQUE_ID>" +
"<STATUS>DONE</STATUS>" +
"</Product>" +
"<Product>" +
"<ID>0701161476418</ID>" +
"<UNIQUE_ID>test26051605</UNIQUE_ID>" +
"<STATUS>DONE</STATUS>" +
"</Product>" +
"</Products>";
XDocument doc = XDocument.Parse(xml);
var results = doc.Descendants("Product").Select(x => new
{
id = (long)x.Element("ID"),
uniqueID = (string)x.Element("UNIQUE_ID"),
status = (string)x.Element("STATUS")
}).ToList();
}
}
}
This could work, but I guess there is a better solution:
XDocument xm = XDocument.Parse(XML_STRING);
var product = xm.Element("Product").Element("Product");
string uniqueID = product.Element("UNIQUE_ID").Value;
string status = product.Element("STATUS").Value;
Your SelectNodes line seemed wrong for the sample Xml.
XmlNodeList xnList = xm.SelectNodes("/Product");
if (xnList.Count > 0)
{
string uniqueID = xnList[0]["UNIQUE_ID"].InnerText;
string status = xnList[0]["STATUS"].InnerText;
}

NetSuite SuiteTalk "customSearchJoin" access from SOAP XML Response

I am still getting warmed up with NetSuite and have come across an issue I can't crack.
I am building a C# function to pull information from an XML soap response from NetSuite. This XML is the result of a saved search call which contains a section that is a join titled customSearchJoin that I am unsure how to access. Below I will show the XML section and my attempts to access it in the C# function.
I am attempting to access the field with the value of 091736418
XML segment:
<platformCore:searchRow xsi:type="tranSales:TransactionSearchRow" xmlns:tranSales="urn:sales_2014_1.transactions.webservices.netsuite.com">
<tranSales:basic xmlns:platformCommon="urn:common_2014_1.platform.webservices.netsuite.com">
<platformCommon:dateCreated>
<platformCore:searchValue>2015-12-17T08:43:00.000-08:00</platformCore:searchValue>
</platformCommon:dateCreated>
<platformCommon:entity>
<platformCore:searchValue internalId="615"/>
</platformCommon:entity>
</tranSales:basic>
<tranSales:customerMainJoin xmlns:platformCommon="urn:common_2014_1.platform.webservices.netsuite.com">
<platformCommon:altName>
<platformCore:searchValue>Some Specific Customer</platformCore:searchValue>
</platformCommon:altName>
</tranSales:customerMainJoin>
<tranSales:itemJoin xmlns:platformCommon="urn:common_2014_1.platform.webservices.netsuite.com">
<platformCommon:itemId>
<platformCore:searchValue>Some Product</platformCore:searchValue>
</platformCommon:itemId>
</tranSales:itemJoin>
<tranSales:customSearchJoin xmlns:platformCommon="urn:common_2014_1.platform.webservices.netsuite.com">
<platformCommon:customizationRef internalId="167" scriptId="custrecord_itmfulfillmentid"/>
<platformCommon:searchRowBasic xsi:type="platformCommon:CustomRecordSearchRowBasic">
<platformCommon:recType internalId="25"/>
<platformCommon:customFieldList>
<platformCore:customField xsi:type="platformCore:SearchColumnStringCustomField" scriptId="custrecord_ssccbarcode" internalId="169">
<platformCore:searchValue>091736418</platformCore:searchValue>
</platformCore:customField>
</platformCommon:customFieldList>
</platformCommon:searchRowBasic>
</tranSales:customSearchJoin>
</platformCore:searchRow>
C# function:
private void testCustomJoinSearch() {
TransactionSearchAdvanced transSearchAdv = new TransactionSearchAdvanced
{
savedSearchScriptId = "customsearch_savedSearchID"
};
SearchResult searchResult = _service.search(transSearchAdv);
if (searchResult.status.isSuccess)
{
Console.WriteLine("Search Success");
foreach (TransactionSearchRow transSearchRow in searchResult.searchRowList)
{
// declare vars
string transInternalID = "";
string ssccBarcode = "";
//normal field
transInternalID = transSearchRow.basic. internalId[0].searchValue.internalId.ToString();
//joined and custom field ? Not sure this loop is correct
foreach (CustomSearchRowBasic customBasicSearchRow in transSearchRow.customSearchJoin)
{
// ????
};
Console.WriteLine("transInternalID {0}", transInternalID);
Console.WriteLine("ssccBarcode {0}", ssccBarcode);
} // end main search row
}
else
{
Console.WriteLine("Search Failure");
Console.WriteLine(searchResult.status.statusDetail);
}
}
Try xml linq. I fixed your xml that was missing some namespace definitions so I added root and then closed the end tags platformCore:searchRow and root.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication70
{
class Program
{
static void Main(string[] args)
{
string xml =
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<Root xmlns:platformCore=\"abc\" xmlns:xsi=\"def\">" +
"<platformCore:searchRow xsi:type=\"tranSales:TransactionSearchRow\" xmlns:tranSales=\"urn:sales_2014_1.transactions.webservices.netsuite.com\">" +
"<tranSales:basic xmlns:platformCommon=\"urn:common_2014_1.platform.webservices.netsuite.com\">" +
"<platformCommon:dateCreated>" +
"<platformCore:searchValue>2015-12-17T08:43:00.000-08:00</platformCore:searchValue>" +
"</platformCommon:dateCreated>" +
"<platformCommon:entity>" +
"<platformCore:searchValue internalId=\"615\"/>" +
"</platformCommon:entity>" +
"</tranSales:basic>" +
"<tranSales:customerMainJoin xmlns:platformCommon=\"urn:common_2014_1.platform.webservices.netsuite.com\">" +
"<platformCommon:altName>" +
"<platformCore:searchValue>Some Specific Customer</platformCore:searchValue>" +
"</platformCommon:altName>" +
"</tranSales:customerMainJoin>" +
"<tranSales:itemJoin xmlns:platformCommon=\"urn:common_2014_1.platform.webservices.netsuite.com\">" +
"<platformCommon:itemId>" +
"<platformCore:searchValue>Some Product</platformCore:searchValue>" +
"</platformCommon:itemId>" +
"</tranSales:itemJoin>" +
"<tranSales:customSearchJoin xmlns:platformCommon=\"urn:common_2014_1.platform.webservices.netsuite.com\">" +
"<platformCommon:customizationRef internalId=\"167\" scriptId=\"custrecord_itmfulfillmentid\"/>" +
"<platformCommon:searchRowBasic xsi:type=\"platformCommon:CustomRecordSearchRowBasic\">" +
"<platformCommon:recType internalId=\"25\"/>" +
"<platformCommon:customFieldList>" +
"<platformCore:customField xsi:type=\"platformCore:SearchColumnStringCustomField\" scriptId=\"custrecord_ssccbarcode\" internalId=\"169\">" +
"<platformCore:searchValue>091736418</platformCore:searchValue>" +
"</platformCore:customField>" +
"</platformCommon:customFieldList>" +
"</platformCommon:searchRowBasic>" +
"</tranSales:customSearchJoin>" +
"</platformCore:searchRow>" +
"</Root>";
XDocument doc = XDocument.Parse(xml);
string searchvalue = doc.Descendants().Where(x => x.Name.LocalName == "customSearchJoin")
.Descendants().Where(y => y.Name.LocalName == "searchValue").Select(z => (string)z).FirstOrDefault();
}
}
}
I believe it is something like this:
foreach (CustomSearchRowBasic customBasicSearchRow in transSearchRow.customSearchJoin) {
// ????
CustomRecordSearchRowBasic itmfulfillmentidRecord = customBasicSearchRow.searchRowBasic as CustomRecordSearchRowBasic;
foreach(SearchColumnCustomField customField in itmfulfillmentidRecord.customFieldList) {
if (customField.scriptId == "custrecord_ssccbarcode") {
SearchColumnStringCustomField stringField = customField as SearchColumnStringCustomField;
string itmfulfillmentid = stringField.searchValue;
}
}
}

Reading xml document with several nodes

I have used the following code before but my xml is different this time:
protected string ReturnXmlValue(XmlDocument myXDoc, string field)
{
var retval = string.Empty;
try
{
var node = myXDoc.GetElementsByTagName(field);
if (node.Count > 0)
{
var xmlNode = node.Item(0);
if (xmlNode != null)
{
retval = xmlNode.InnerText;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
throw;
}
return retval;
}
Here is an example of my xml file dummied down a bit:
<RichDBDS>
<TrxDetailCard>
<TRX_HD_Key>18683435</TRX_HD_Key>
<Date_DT>2015-10-22T21:32:00.233+00:00</Date_DT>
<TRX_Card_Key>15263569</TRX_Card_Key>
<Total_Amt_MN>22.0000</Total_Amt_MN>
<Result_CH>0 </Result_CH>
<Result_Txt_VC>APPROVED</Result_Txt_VC>
<Approval_Code_CH>0943253</Approval_Code_CH>
</TrxDetailCard>
<TrxDetailCard>
<TRX_HD_Key>18683825</TRX_HD_Key>
<Date_DT>2015-10-23T21:32:00.233+00:00</Date_DT>
<TRX_Card_Key>15263569</TRX_Card_Key>
<Total_Amt_MN>32.0000</Total_Amt_MN>
<Result_CH>0 </Result_CH>
<Result_Txt_VC>APPROVED</Result_Txt_VC>
<Approval_Code_CH>093389</Approval_Code_CH>
</TrxDetailCard>
</RichDBDS>
I've not worked with xml much so I'm not sure how to search this for a specific amount. I can have several TrxDetailCards. I know how to get amount when I only have one TrxDetailCard but I need to return the TrxDetailCard for the hits on the amount that I need.
So if I am looking for the TrxDetailCard that is 32.00, I need the method to return:
<TrxDetailCard>
<TRX_HD_Key>18683825</TRX_HD_Key>
<Date_DT>2015-10-23T21:32:00.233+00:00</Date_DT>
<TRX_Card_Key>15263569</TRX_Card_Key>
<Total_Amt_MN>32.0000</Total_Amt_MN>
<Result_CH>0 </Result_CH>
<Result_Txt_VC>APPROVED</Result_Txt_VC>
<Approval_Code_CH>093389</Approval_Code_CH>
</TrxDetailCard>
How would I go about doing this?
Try this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
"<RichDBDS>" +
"<TrxDetailCard>" +
"<TRX_HD_Key>18683435</TRX_HD_Key>" +
"<Date_DT>2015-10-22T21:32:00.233+00:00</Date_DT>" +
"<TRX_Card_Key>15263569</TRX_Card_Key>" +
"<Total_Amt_MN>22.0000</Total_Amt_MN>" +
"<Result_CH>0 </Result_CH>" +
"<Result_Txt_VC>APPROVED</Result_Txt_VC>" +
"<Approval_Code_CH>0943253</Approval_Code_CH>" +
"</TrxDetailCard>" +
"<TrxDetailCard>" +
"<TRX_HD_Key>18683825</TRX_HD_Key>" +
"<Date_DT>2015-10-23T21:32:00.233+00:00</Date_DT>" +
"<TRX_Card_Key>15263569</TRX_Card_Key>" +
"<Total_Amt_MN>32.0000</Total_Amt_MN>" +
"<Result_CH>0 </Result_CH>" +
"<Result_Txt_VC>APPROVED</Result_Txt_VC>" +
"<Approval_Code_CH>093389</Approval_Code_CH>" +
"</TrxDetailCard>" +
"</RichDBDS>";
XElement richDBDS = XElement.Parse(xml);
XElement results = richDBDS.Elements("TrxDetailCard").Where(x => (decimal)x.Element("Total_Amt_MN") == (decimal)32.0000).FirstOrDefault();
}
}
}
​
You can use Linq-to-Xml
var str = File.ReadAllText(#"C:\YourDirectory\sample.xml");
XDocument xDoc = XDocument.Parse(str);
var trxNodes = xDoc.Descendants("TrxDetailCard");
var node = trxNodes.First(n => double.Parse(n.Element("Total_Amt_MN").Value) == 32.00);

Entity Framework not writing to Database

I'm trying to take a JSON response, deserialize it, and store in a localdb. Everything is working, but its not writing the data to my database. I can't seem to figure out why its not as I'm not getting any errors.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using DataFetcher.DataFieldsInClasses;
using DataFetcher.EF_Models;
using Newtonsoft.Json;
namespace DataFetcher
{
class Program
{
static void Main(string[] args)
{
var teamNames = new TeamsList();
teamNames.teamList.Sort();
for (int i = 0; i < 30; i++)
{
var cities = teamNames.teamList.ElementAt(i);
//URL for each indivual team
string URL = #"http://nhlwc.cdnak.neulion.com/fs1/nhl/league/teamroster/" + cities + "/iphone/clubroster.json";
WebRequest wrGETURL;
wrGETURL = WebRequest.Create(URL);
HttpWebResponse response = wrGETURL.GetResponse() as HttpWebResponse;
StreamReader responseStream = new StreamReader(response.GetResponseStream());
var result = responseStream.ReadToEnd();
//var parsedInformation = JsonConvert.DeserializeObject<RootObject>(result);
foreach (var forwardData in JsonConvert.DeserializeObject<RootObject>(result).forwards)
{
//test output
Console.WriteLine(forwardData.id + " " + " " + forwardData.position + " " + forwardData.name + " " + forwardData.twitterURL + " " + forwardData.height + " " + forwardData.birthdate);
//write to database using EF
using (var _temp_Player = new DataFetcherDBEntities())
{
//var players = _temp_Player.Set<Player>();
_temp_Player.Players.Attach(new Player
{
player_id = forwardData.id,
name = forwardData.name,
age = forwardData.age,
birthdate = forwardData.birthdate,
birthplace = forwardData.birthplace,
weight = forwardData.weight,
height = forwardData.height,
number = forwardData.number,
position = forwardData.position,
imageUrl = forwardData.imageUrl,
twitterURL = forwardData.twitterURL,
twitterHandle = forwardData.twitterHandle
});
_temp_Player.SaveChanges();
}
}
}
Console.ReadKey();
}
}
}
Any tips/ideas?
P.S I'm still learning and self-taught.
*Edit, I copied the localdb to "...DataFetcher\bin\Debug\Databases" and my new error is ""Violation of PRIMARY KEY constraint 'PK__tmp_ms_x__44DA120C6655986D'. Cannot insert duplicate key in object 'dbo.Player'. The duplicate key value is (8473492).\r\nThe statement has been terminated."}"
Which doesn't make sense to me as every player has a unique ID (player_id = forwardData.id)
You need to use _temp_Player.Players.Add instead of Attach.
When you Attach, you need to set the entity state for the EF to detect that it is a new record and insert it into the database during Save Changes.

GetListItems() causes a 401 error but Checkout() works

I want to get the contents of a Sharepoint 2007 list via web service. I'm using this code, which I mostly copied from the MSDN page for GetListItems:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace testGetListItems
{
class Program
{
static void Main(string[] args)
{
sharepoint.Lists listService = new sharepoint.Lists();
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
XmlDocument xmlDoc = new System.Xml.XmlDocument();
XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
ndQueryOptions.InnerXml =
"<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>" +
"<DateInUtc>TRUE</DateInUtc>";
ndViewFields.InnerXml = "<FieldRef Name='Field1' /> <FieldRef Name='Field2'/>";
ndQuery.InnerXml = "<Where><And><Gt><FieldRef Name='Field1'/>" +
"<Value Type='Number'>5000</Value></Gt><Gt><FieldRef Name='Field2'/>" +
"<Value Type= 'DateTime'>2003-07-03T00:00:00</Value></Gt></And></Where>";
try
{
bool checkoutResult=listService.CheckOutFile("http://sharepoint/sites/mysite/myFile.xlsx", "false", null);
XmlNode ndListItems =
listService.GetListItems("Test List", null, ndQuery,
ndViewFields, null, ndQueryOptions, null);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
Console.WriteLine("Message:\n" + ex.Message + "\nDetail:\n" +
ex.Detail.InnerText +
"\nStackTrace:\n" + ex.StackTrace);
}
}
}
}
The call to CheckOutFile() works correctly. But the GetListItems() call gives me this error:
An unhandled exception of type 'System.Net.WebException' occurred in System.Web.Services.dll
Additional information: The request failed with HTTP status 401: Unauthorized.
I don't understand why CheckOutFile() succeeds but GetListItems() fails, especially because the document that I'm checking out is in the list that's being accessed by GetListItems().
UPDATE: This worked in a test console app, but not in my main application. Leaving this answer up for now, but I'm not going to accept it until I get this fixed.
It turned out the problem was with the URL of the web service. Even though I had added it as:
http://sharepoint/sites/mySite/_vti_bin/Lists.asmx
the app.config file had it listed as:
http://sharepoint/_vti_bin/Lists.asmx
This remained a problem even after I deleted the reference and re-added it; I had to change the app.config contents manually. Once I'd done so, the GetListItems() call succeeded.
You must set a user and password who can get the items in the library/list this way (for me it works, you could try):
public string GetIDFromList(string parameter)
{
string retorno = "";
try
{
string path = "http://www.test.com/web/";
// Reference to the SharePoint Lists web service:
WSSharePointCSCLists.Lists listsWS = new WSSharePointCSCLists.Lists();
listsWS.Url = path + "_vti_bin/lists.asmx";
listsWS.Credentials = GetUserCredential();
string listName = "MyList";
string viewName = "";
//string webID;
string rowLimit = "500";
// Web ID:
webID = "098304-9098asdf-qwelkfj-eoqiula";
XmlDocument xmlDoc = new System.Xml.XmlDocument();
// Query em CAML (SharePoint):
XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
XmlNode ndViewFields =
xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
XmlNode ndQueryOptions =
xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
ndQueryOptions.InnerXml =
"<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>" +
"<DateInUtc>TRUE</DateInUtc>" +
"<ViewAttributes Scope=\"RecursiveAll\" />";
ndViewFields.InnerXml = #"<FieldRef Name='Title' />
<FieldRef Name='ID' />";
string caml =
String.Format(
#"<Where>
<Contains>
<FieldRef Name='MyColumn' />
<Value Type='Text'>{0}</Value>
</Contains>
</Where>",
parameter);
ndQuery.InnerXml = caml;
XmlNode retornoWS = listsWS.GetListItems(listName, null, ndQuery,
ndViewFields, rowLimit,
ndQueryOptions, webID);
retorno = retornoWS.InnerXml;
}
catch (Exception ex)
{
Debug.WriteLine("Exception: " + ex.Message);
throw;
}
return retorno;
}
public NetworkCredential GetUserCredential()
{
return new System.Net.NetworkCredential("<username>",
"<password>",
"<domain>");
}

Categories

Resources