How to read signed request using c# 4.0 - c#

I am using c# 4.0 and I am integrating facebook registration plugin
Can somebody please tell me what I need to do to read signed request.
I have downloaded Facebook.dll and Newtonsoft.Json.dll
I also have valid
App ID: ***
API Key: **********
App Secret: *************
If possible please give me a sample code how should I pass these keyes and how I can collect the decoded data sent in form of signed request.
Thanks:

There must be more easy ways to read signed request following is what I am using. There are few steps involved to read facebook signed request in c#
Dot net 2010 is required to follow these steps. It is suggested if you make a new web based project named “fb” when you done then you may import this code to your real project.
Download source from http://facebooksdk.codeplex.com/SourceControl/changeset/view/f8109846cba5#Source%2fFacebook%2fFacebookApp.cs
After unzip you will get “facebooksdk-f8109846cba5” inside it you will find a folder facebooksdk-f8109846cba5\Source\Facebook in this folder look for “Facebook.csproj”
Open “Facebook.csproj” in vs 2010 Look for file “FacebookApp.cs” open this file and search “internal protected FacebookSignedRequest ParseSignedRequest(string signedRequestValue)
Change “internal protected” to “public”
Then build the project by right click on project. Now it’s complied file (“Facebook.dll”) is ready to use. Copy it to your project bin directory and add its reference.
Now download Json.Net 3.5 release 8 from http://json.codeplex.com/releases/view/50552 and add to your project bin folder and also add its reference.
Now you are ready to read signed request. It is time to write code to read signed request.
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Facebook;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System.Collections.Generic;
namespace fb
{
public partial class test3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FacebookApp fap = new FacebookApp();
fap.AppId = "************";
fap.AppSecret = "********************";
string requested_Data = Request.Form["signed_request"];
FacebookSignedRequest fsr = fap.ParseSignedRequest(requested_Data);
// string json = JsonConvert.SerializeObject(fsr.Dictionary, Formatting.Indented);
UserData ud = new UserData(fsr);
Response.Write(ud.name + "");
Response.Write(ud.birthday + "");
Response.Write(ud.country + "");
Response.Write(ud.email + "");
Response.Write(ud.gender + "");
Response.Write(ud.location + "");
Response.Write(ud.userId + "");
}
}
public class UserData
{
public UserData(FacebookSignedRequest fsr)
{
string value = string.Empty;
JObject o;
foreach (string key in fsr.Dictionary.Keys)
{
value = fsr.Dictionary[key];
switch (key)
{
case "user_id":
userId = value;
break;
case "registration":
o = JObject.Parse(value);
name = GetValue(o, "name");
birthday = GetValue(o, "birthday");
email = GetValue(o, "email");
gender = GetValue(o, "gender");
location = GetValue(o, "location.name");
break;
case "user":
o = JObject.Parse(value);
country = GetValue(o, "country");
break;
}
}
}
private string GetValue(JObject o, string token)
{
string ret = string.Empty;
try
{
ret = (string)o.SelectToken(token);
}
catch (Exception ex)
{
throw ex;
}
return ret;
}
public string name { get; set; }
public string birthday { get; set; }
public string gender { get; set; }
public string location { get; set; }
public string country { get; set; }
public string email { get; set; }
public string userId { get; set; }
}
}
This is what I am using and its working fine for me.

Here is using the FB C# SDK v.5.2.1.0
var signed_request = Request.Form["signed_request"];
var fap = Facebook.FacebookApplication.Current;
var signed_request_obj = Facebook.FacebookSignedRequest.Parse(fap, signed_request);
if (signed_request_obj != null)
{
JObject o = JObject.Parse(signed_request_obj.Data.ToString());
}

Related

Taking a JSON array with file paths to update the JSON array for date last modified

I currently have an API which I use dapper to connect to a SQL database to find the file path for documents related to a certain ID. The result is returned in JSON format as follows:
[{"DOC-ID": 1, "DOCUMENT_FULL_PATH": "/PATH/FILENAME.DOC"},{"DOC-ID": 2, "DOCUMENT_FULL_PATH": "/PATH/FILENAME2.DOC"}]
I am trying to get my API to deserialize the JSON data then link that too a model (I prefer to not use models but the only solution I found was using JSON DOM which is briefly discussed on MS website but does not provide an example of how to loop through the JSON array so I could not move forward with this example). When I try to deserialize the dapper query result I get the error indicated below (shown at the line item in the code). I am not sure what is triggering this as I would think the QuerySingle could be deserialized with this method. Once this error is fixed I need to check the files last modified date and save that value to the model which I then again need to serialize to send to the front end! I have spent so much time on this so some help would be much appreciated!
[HttpPost]
public ActionResult MainReviewDocuments([FromForm] string ID)
{
//Using FormData on frontend
//checking ID exists on searching in dashboard
if (ID == null || ID.Length == 0)
{
return Ok(new { Result = "Failed" });
}
else
{
//We have parameters here just in case we want to use them
var UserID = HttpContext.User.FindFirst(ClaimTypes.Name).Value;
String query = "select dbo.A2Q_0132W_RR_IDDocumentation_JSON(#ID) as output";
using (var connection = new SqlConnection(connectionString))
{
var json = connection.QuerySingle<string>(query, new { ID = ID});
MainReviewDocuments? mainreviewdocuments = JsonSerializer.Deserialize<MainReviewDocuments>(json); // this line draws an error 'The JSON value could not be converted to Project.Models.MainReviewDocuments. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'
var rootPath = Path.Combine(Directory.GetParent(env.ContentRootPath).ToString(), "Files");
foreach (var document in mainreviewdocuments)
{
filePath = Path.Combine(rootPath, document.DOCUMENT_FULL_PATH);
//Check file system for each file path and set last modified value to model object LAST_MODIFIED. Struggling with this as well
}
return Ok(mainreviewdocuments); // Can I use Ok() method to convert model back to JSON?
}
}
}
In your original call, you need to de-serialize to a List:
MainReviewDocuments? mainreviewdocuments = JsonSerializer.Deserialize<List<MainReviewDocuments>>(json);
and then access your properties are required.
Using Newtonsoft.Json library:
You can de-serialize your JSON string that you receive from your DB into the following class:
public class MainReviewDocuments
{
[JsonProperty("DOC-ID")]
public int DOCID { get; set; }
public string DOCUMENT_FULL_PATH { get; set; }
}
Or you can use dynamic to de-serialize your JSON:
var mainreviewdocuments = JsonSerializer.Deserialize<dynamic>(json);
and then access the properties as shown in the example below.
You can refer to a working example below:
using System;
using Newtonsoft.Json;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
var myJsonString=#"[{'DOC-ID': 1, 'DOCUMENT_FULL_PATH': '/PATH/FILENAME.DOC'},{'DOC-ID': 2, 'DOCUMENT_FULL_PATH': '/PATH/FILENAME2.DOC'}]";
var mainreviewdocuments =JsonConvert.DeserializeObject<List<MainReviewDocuments>>(myJsonString);
Console.WriteLine("Example using Model: \n");
foreach(var item in mainreviewdocuments)
{
Console.WriteLine(item.DOCID);
Console.WriteLine(item.DOCUMENT_FULL_PATH);
}
Console.WriteLine("\n");
Console.WriteLine("Example using Dynamic: \n");
//Example using dynamic
var mainreviewdocumentsDynamic=JsonConvert.DeserializeObject<dynamic>(myJsonString);
foreach(var item in mainreviewdocumentsDynamic)
{
Console.WriteLine(item["DOC-ID"]);
Console.WriteLine(item["DOCUMENT_FULL_PATH"]);
}
}
}
public class MainReviewDocuments
{
[JsonProperty("DOC-ID")]
public int DOCID { get; set; }
public string DOCUMENT_FULL_PATH { get; set; }
}
Output:
Example using Model:
1
/PATH/FILENAME.DOC
2
/PATH/FILENAME2.DOC
Example using Dynamic:
1
/PATH/FILENAME.DOC
2
/PATH/FILENAME2.DOC
Using System.Text.Json library:
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;
public class Program
{
public static void Main()
{
var myJsonString="[{\"DOC-ID\": 1, \"DOCUMENT_FULL_PATH\": \"/PATH/FILENAME.DOC\"},{\"DOC-ID\": 2, \"DOCUMENT_FULL_PATH\": \"/PATH/FILENAME2.DOC\"}]";
var mainreviewdocuments = JsonSerializer.Deserialize<List<MainReviewDocuments>>(myJsonString);
Console.WriteLine("Example using Model: \n");
foreach(var item in mainreviewdocuments)
{
Console.WriteLine(item.DOCID);
Console.WriteLine(item.DOCUMENT_FULL_PATH);
}
Console.WriteLine("\n");
Console.WriteLine("Example using Dynamic: \n");
using (JsonDocument document = JsonDocument.Parse(myJsonString))
{
foreach (JsonElement element in document.RootElement.EnumerateArray())
{
Console.WriteLine(element.GetProperty("DOC-ID"));
Console.WriteLine(element.GetProperty("DOCUMENT_FULL_PATH"));
}
}
}
}
public class MainReviewDocuments
{
[JsonPropertyName("DOC-ID")]
public int DOCID { get; set; }
public string DOCUMENT_FULL_PATH { get; set; }
}
Output:
Example using Model:
1
/PATH/FILENAME.DOC
2
/PATH/FILENAME2.DOC
Example using Dynamic:
1
/PATH/FILENAME.DOC
2
/PATH/FILENAME2.DOC
Working example: https://dotnetfiddle.net/nEjPIK
You can read more on the comparison between the two libraries in this article

C# Having Trouble Asynchronously Downloading Multiple Files in Parallel on Console Application [duplicate]

This question already has answers here:
My console app shutdown prematurely when using async / await?
(4 answers)
Program exits upon calling await
(3 answers)
Closed 2 years ago.
Before you all go on a rampage about how this is a duplicate question, I have spent two days working on this issue, watching youtube tutorials on asynchronous programming, surfing similar stackoverflow posts etc, and I cannot for the life of me figure out how to apply Asynchronous Parallel Downloading of files into my project.
First things first, some background:
I am creating a program that, when given a query input via the user, will make a call to the twitch API and download clips.
My program is two parts
1- A web scraper that generates a .json file with all details needed to download files and
2 - A downloader.
Part 1 works perfectly fine and generates the .json files no trouble.
My Downloader contains reference to a Data class that is a handler for common properties and methods like my ClientID, Authentication, OutputPath, JsonFile, QueryURL. It also contains methods to give values to these properties.
Here are the two methods of my FileDownloader.cs that are the problem:
public async static void DownloadAllFiles(Data clientData)
{
data = clientData;
data.OutputFolderExists();
// Deserialize .json file and get ClipInfo list
List<ClipInfo> clips = JsonConvert.DeserializeObject<List<ClipInfo>>(File.ReadAllText(data.JsonFile));
tasks = new List<Task>();
foreach(ClipInfo clip in clips)
{
tasks.Add(DownloadFilesAsync(clip));
}
await Task.WhenAll(tasks);
}
private async static Task DownloadFilesAsync(ClipInfo clip)
{
WebClient client = new WebClient();
string url = GetClipURL(clip);
string filepath = data.OutputPath + clip.id + ".mp4";
await client.DownloadFileTaskAsync(new Uri(url), filepath);
}
This is only one of my many attempts of downloading files, one which I got the idea from this post:
stackoverflow_link
I have also tried methods like the following from a YouTube video by IAmTimCorey:
video_link
I have spent many an hour tackling this problem, and I honestly can't figure out why it won't work with any of my attempts. I would vastly appreciate your help.
Thanks,
Ben
Below is the entirety of my code, should anyone need it for any reason.
Code Structure:
The only external libraries I have downloaded is Newtonsoft.Json
ClipInfo.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Downloader
{
public class ClipInfo
{
public string id { get; set; }
public string url { get; set; }
public string embed_url { get; set; }
public string broadcaster_id { get; set; }
public string broadcaster_name { get; set; }
public string creator_id { get; set; }
public string creator_name { get; set; }
public string video_id { get; set; }
public string game_id { get; set; }
public string language { get; set; }
public string title { get; set; }
public int view_count { get; set; }
public DateTime created_at { get; set; }
public string thumbnail_url { get; set; }
}
}
Pagination.cs
namespace Downloader
{
public class Pagination
{
public string cursor { get; set; }
}
}
Root.cs
using System.Collections.Generic;
namespace Downloader
{
public class Root
{
public List<ClipInfo> data { get; set; }
public Pagination pagination { get; set; }
}
}
Data.cs
using System;
using System.IO;
namespace Downloader
{
public class Data
{
private static string directory = Directory.GetCurrentDirectory();
private readonly static string defaultJsonFile = directory + #"\clips.json";
private readonly static string defaultOutputPath = directory + #"\Clips\";
private readonly static string clipsLink = "https://api.twitch.tv/helix/clips?";
public string OutputPath { get; set; }
public string JsonFile { get; set; }
public string ClientID { get; private set; }
public string Authentication { get; private set; }
public string QueryURL { get; private set; }
public Data()
{
OutputPath = defaultOutputPath;
JsonFile = defaultJsonFile;
}
public Data(string clientID, string authentication)
{
ClientID = clientID;
Authentication = authentication;
OutputPath = defaultOutputPath;
JsonFile = defaultJsonFile;
}
public Data(string clientID, string authentication, string outputPath)
{
ClientID = clientID;
Authentication = authentication;
OutputPath = directory + #"\" + outputPath + #"\";
JsonFile = OutputPath + outputPath + ".json";
}
public void GetQuery()
{
Console.Write("Please enter your query: ");
QueryURL = clipsLink + Console.ReadLine();
}
public void GetClientID()
{
Console.WriteLine("Enter your client ID");
ClientID = Console.ReadLine();
}
public void GetAuthentication()
{
Console.WriteLine("Enter your Authentication");
Authentication = Console.ReadLine();
}
public void OutputFolderExists()
{
if (!Directory.Exists(OutputPath))
{
Directory.CreateDirectory(OutputPath);
}
}
}
}
JsonGenerator.cs
using System;
using System.IO;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Linq;
namespace Downloader
{
public static class JsonGenerator
{
// This class has no constructor.
// You call the Generate methods, passing in all required data.
// The file will then be generated.
private static Data data;
public static async Task Generate(Data clientData)
{
data = clientData;
string responseContent = null;
// Loop that runs until the api request goes through
bool authError = true;
while (authError)
{
authError = false;
try
{
responseContent = await GetHttpResponse();
}
catch (HttpRequestException)
{
Console.WriteLine("Invalid authentication, please enter client-ID and authentication again!");
data.GetClientID();
data.GetAuthentication();
authError = true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
authError = true;
}
}
data.OutputFolderExists();
GenerateJson(responseContent);
}
// Returns the contents of the resopnse to the api call as a string
private static async Task<string> GetHttpResponse()
{
// Creating client
HttpClient client = new HttpClient();
if (data.QueryURL == null)
{
data.GetQuery();
}
// Setting up request
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, data.QueryURL);
// Adding Headers to request
requestMessage.Headers.Add("client-id", data.ClientID);
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", data.Authentication);
// Receiving response to the request
HttpResponseMessage responseMessage = await client.SendAsync(requestMessage);
// Gets the content of the response as a string
string responseContent = await responseMessage.Content.ReadAsStringAsync();
return responseContent;
}
// Generates or adds to the .json file that contains data on each clip
private static void GenerateJson(string responseContent)
{
// Parses the data from the response to the api request
Root responseResult = JsonConvert.DeserializeObject<Root>(responseContent);
// If the file doesn't exist, we need to create it and add a '[' at the start
if (!File.Exists(data.JsonFile))
{
FileStream file = File.Create(data.JsonFile);
file.Close();
// The array of json objects needs to be wrapped inside []
File.AppendAllText(data.JsonFile, "[\n");
}
else
{
// For a pre-existing .json file, The last object won't have a comma at the
// end of it so we need to add it now, before we add more objects
string[] jsonLines = File.ReadAllLines(data.JsonFile);
File.WriteAllLines(data.JsonFile, jsonLines.Take(jsonLines.Length - 1).ToArray());
File.AppendAllText(data.JsonFile, ",");
}
// If the file already exists, but there was no [ at the start for whatever reason,
// we need to add it
if (File.ReadAllText(data.JsonFile).Length == 0 || File.ReadAllText(data.JsonFile)[0] != '[')
{
File.WriteAllText(data.JsonFile, "[\n" + File.ReadAllText(data.JsonFile));
}
string json;
// Loops through each ClipInfo object that the api returned
for (int i = 0; i < responseResult.data.Count; i++)
{
// Serializes the ClipInfo object into a json style string
json = JsonConvert.SerializeObject(responseResult.data[i]);
// Adds the serialized contents of ClipInfo to the .json file
File.AppendAllText(data.JsonFile, json);
if (i != responseResult.data.Count - 1)
{
// All objects except the last require a comma at the end of the
// object in order to correctly format the array of json objects
File.AppendAllText(data.JsonFile, ",");
}
// Adds new line after object entry
File.AppendAllText(data.JsonFile, "\n");
}
// Adds the ] at the end of the file to close off the json objects array
File.AppendAllText(data.JsonFile, "]");
}
}
}
FileDownloader.cs
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
namespace Downloader
{
public class FileDownloader
{
private static Data data;
private static List<Task> tasks;
public async static void DownloadAllFiles(Data clientData)
{
data = clientData;
data.OutputFolderExists();
// Deserialize .json file and get ClipInfo list
List<ClipInfo> clips = JsonConvert.DeserializeObject<List<ClipInfo>>(File.ReadAllText(data.JsonFile));
tasks = new List<Task>();
foreach (ClipInfo clip in clips)
{
tasks.Add(DownloadFilesAsync(clip));
}
await Task.WhenAll(tasks);
}
private static void GetData()
{
if (data.ClientID == null)
{
data.GetClientID();
}
if (data.Authentication == null)
{
data.GetAuthentication();
}
if (data.QueryURL == null)
{
data.GetQuery();
}
}
private static string GetClipURL(ClipInfo clip)
{
// Example thumbnail URL:
// https://clips-media-assets2.twitch.tv/AT-cm%7C902106752-preview-480x272.jpg
// You can get the URL of the location of clip.mp4
// by removing the -preview.... from the thumbnail url */
string url = clip.thumbnail_url;
url = url.Substring(0, url.IndexOf("-preview")) + ".mp4";
return url;
}
private async static Task DownloadFilesAsync(ClipInfo clip)
{
WebClient client = new WebClient();
string url = GetClipURL(clip);
string filepath = data.OutputPath + clip.id + ".mp4";
await client.DownloadFileTaskAsync(new Uri(url), filepath);
}
private static void FileDownloadComplete(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
tasks.Remove((Task)sender);
}
}
}
Program.cs
using System;
using System.Threading.Tasks;
using Downloader;
namespace ClipDownloader
{
class Program
{
private static string clientID = "{your_client_id}";
private static string authentication = "{your_authentication}";
async static Task Main(string[] args)
{
Console.WriteLine("Enter your output path");
string outputPath = Console.ReadLine();
Data data = new Data(clientID, authentication, outputPath);
Console.WriteLine(data.OutputPath);
//await JsonGenerator.Generate(data);
FileDownloader.DownloadAllFiles(data);
}
}
}
The example query I usually type in is "game_id=510218"
async void is your problem
Change
public static async void DownloadAllFiles(Data clientData)
To
public static async Task DownloadAllFiles(Data clientData)
Then you can await it
await FileDownloader.DownloadAllFiles(data);
The longer story:
async void runs unobserved (fire and forget). You can't wait for them to finish. In essence as soon as your program starts the task, it finishes, and tears down the App Domain and all your sub tasks, leading you to believe nothing is working.
I'm trying to stay on topic here as best as I can, but when using JsonConvert.DeserializeObject{T}, isn't T suppose to be an encapsulating root object type? I have never used it the way you're using it, so I'm just curious if that might be your bug. I could be completely wrong, and spare me if i am, but JSON is key:value based. Deserializing directly to a List doesn't really make sense. Unless there is a special case in the deserializer? List would be a file that's purely an array of ClipInfo values being deserialized into the members of List{T}(private T[] _items, private int _size, etc.) It needs a parent root object.
// current JSON file format implication(which i dont think is valid JSON?(correct me please)
clips:
[
// clip 1
{ "id": "", "url": "" },
// clip N
{ "id": "", "url": "" },
]
// correct(?) JSON file format
{ // { } is the outer encasing object
clips:
[
// clip 1
{ "id": "", "url": "" },
// clip N
{ "id": "", "url": "" },
]
}
class ClipInfoJSONFile
{
public List<ClipInfo> Info { get; set; }
}
var clipInfoList = JsonConverter.DeserializeObject<ClipInfoJSONFile>(...);

uri class not shown in system namespace

I'm trying to compile the following code using the .NET 4.5 Framework:
using System;
using System.Collections.Generic;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Discussion.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
class CodeReview
{
public List<CodeReviewComment> GetCodeReviewComments(int workItemId)
{
List<CodeReviewComment> comments = new List<CodeReviewComment>();
Uri tfsuri = new Uri(MYURL);
TeamFoundationDiscussionService service = new TeamFoundationDiscussionService();
service.Initialize(new Microsoft.TeamFoundation.Client.TfsTeamProjectCollection(tfsuri));
IDiscussionManager discussionManager = service.CreateDiscussionManager();
IAsyncResult result = discussionManager.BeginQueryByCodeReviewRequest(workItemId, QueryStoreOptions.ServerAndLocal, new AsyncCallback(CallCompletedCallback), null);
var output = discussionManager.EndQueryByCodeReviewRequest(result);
foreach (DiscussionThread thread in output)
{
if (thread.RootComment != null)
{
CodeReviewComment comment = new CodeReviewComment();
comment.Author = thread.RootComment.Author.DisplayName;
comment.Comment = thread.RootComment.Content;
comment.PublishDate = thread.RootComment.PublishedDate.ToShortDateString();
comment.ItemName = thread.ItemPath;
comments.Add(comment);
}
}
return comments;
}
static void CallCompletedCallback(IAsyncResult result)
{
// Handle error conditions here
}
public class CodeReviewComment
{
public string Author { get; set; }
public string Comment { get; set; }
public string PublishDate { get; set; }
public string ItemName { get; set; }
}
}
The Visual Studio compiler is complaining that "the type or namespace 'Uri' could not be found" and when I expand the System namespace in the Object Browser it doesnt list any of the Uri classes. I've tried a few other versions of .NET and still have the same problem although it does appear if I select the .NET 4.0 Client Profile but then none of the TeamFoundation classes are present in the Microsoft namespace.
I solved my own problem but I don't understand why it worked. In desperation I created another new Project using .NET 4.5 and copied and pasted the code over. It compiled and Uri shows up as a Class in the Object Browser. I have no idea what the difference between the 2 projects is.

Change due date for production order in SAP B1 with the DI SDK

I am trying to change/update the due date of a production order in SAP B1 with the code below:
public static void ChangeDueDateForProductionOrder(SAPB1Credentials credentials, int poAbsEntry, DateTime dueDate)
{
DebugLogger.Log(credentials.CompanyDB, $"Changing due date for production order '{poAbsEntry}' to '{dueDate.ToString(C_DATE_FORMAT_NL)}'.");
using (var sap = new SAPB1Connection(credentials))
{
ProductionOrders productionOrder = sap.Company.GetBusinessObject(BoObjectTypes.oProductionOrders);
if(productionOrder.GetByKey(poAbsEntry))
{
productionOrder.DueDate = dueDate;
if (productionOrder.Update() != 0)
{
var message = $"Error while changing due date for '{poAbsEntry}' to '{dueDate.ToString(C_DATE_FORMAT_NL)}', the following error is given '{sap.Company.GetLastErrorDescription()}'.";
DebugLogger.Log(credentials.CompanyDB, message);
throw new Exception(message);
}
}
else
throw new Exception($"PoId '{poAbsEntry}' does not exists.");
}
}
However, I am getting the following error:
Error while changing due date for '145' to '11-09-2016', the following error is given 'Field cannot be updated (ODBC -1029)'.
And the error SAP gives is:
Field cannot be updated (ODBC -1029)
Additional information:
It is a new production order that has the status Planned.
Creation date is Today.
Due date I am trying to change it to is Today + 5 days.
The Id (AbsEntry) for the production order is 145.
SAP Business One 9.1
Yes, I can alter the due date in the SAP B1 GUI without problems.
Short, Self Contained, Correct (Compilable), Example
The code below gives me the exact same error. Replaced connection settings with ??.
using System;
using SAPbobsCOM; // Reference to SAP B1 DI SDK (32-bit)
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var credentials = new SAPB1Credentials
{
Server = "??",
CompanyDB = "??",
Username = "??",
Password = "??"
};
SAPbobsCOM.Company sap = new SAPbobsCOM.Company();
sap.Server = credentials.Server;
sap.DbServerType = credentials.ServerType;
sap.CompanyDB = credentials.CompanyDB;
sap.UserName = credentials.Username;
sap.Password = credentials.Password;
sap.language = credentials.Language;
sap.UseTrusted = credentials.UseTrusted;
var returnCode = sap.Connect();
if (returnCode != 0)
{
var error = sap.GetLastErrorDescription();
throw new Exception(error);
}
ProductionOrders productionOrder = sap.GetBusinessObject(BoObjectTypes.oProductionOrders);
if (productionOrder.GetByKey(145))
{
productionOrder.DueDate = DateTime.Now.AddDays(5);
if (productionOrder.Update() != 0)
{
var error = sap.GetLastErrorDescription();
throw new Exception(error);
}
}
else
throw new Exception($"PoId does not exists.");
}
public class SAPB1Credentials
{
public string Server { get; set; }
public string CompanyDB { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public BoSuppLangs Language { get; set; } = BoSuppLangs.ln_English;
public BoDataServerTypes ServerType { get; set; } = BoDataServerTypes.dst_MSSQL2008;
public bool UseTrusted { get; set; } = false;
}
}
}
What am I missing, and why do I get this error?
From the linked SAP thread in the comment.
Edy Simon said:
Hi Lars,
Must be a Patch Level bug.
I can use your code to update it on SBO 881PL6.
You might want to test it on another version.
Regards
Edy
Which resolved the issue.

Neo4jClient Create and Update a Relationship

I have a Neo4j Graphdatabase with access via the Neo4jClient. (It is a .NET client for the REST api of Neo4j)
There is the beginning of a documentation.
What I have done
The connection to the database works.
Client = new GraphClient(new Uri("http://localhost:7474/db/data"));
Client.Connect();
This way I can insert Nodes...
Client.Create(new myNodeClass { name = "Nobody" });
... and query them.
Node<myNodeClass> Node = Client.Get<WordNode>(138);
return Node.Data.name;
What I want to do
I simply want to add and update relationships between Nodes. (The type of relationship have to be numeric.)
Unfortunately there is no documentation about relationships yet.
There is a command named CreateRelationship. But I can't get it work.
Client.CreateRelationship(Neo4jClient.NodeReference<TSourceNode>, TRelationship);
Can you give me an example of adding and updating (numeric) relationships?
There's a lot to be found in the test cases... Such as this:
http://hg.readify.net/neo4jclient/src/4693da483a90/Test/ApiUsageIdeas.cs
I was stuck too then I realized I needed to specify the type parameter of the source node reference parameter in the CreateRelationship method.
In this example, I have created the relationship. I have not yet updated the relationship.
Disclosure(It works on my machine as a console application running visual studio 2012, YMMV)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using Neo4jClient;
namespace Neo4jClientExample
{
class MyConsoleProgram
{
private GraphClient Client {get;set; }
static void Main(string[] args)
{
try{
GraphClient client = new GraphClient(new Uri("http://localhost:7474/db/data"));
client.Connect();
Us us = new Us { Name = "We are Us" };
NodeReference<Us> usRef = client.Create(us);
Console.WriteLine("us node.id: {0}", usRef.Id);
var queryUs = client.Cypher.Start("n", "node(" + usRef.Id + ")").Return<Node<Us>>("n");
Console.WriteLine("Us node name: {0}\n", queryUs.Results.AsEnumerable<Node<Us>>().First().Data);
AllYourBase allYourBase = new AllYourBase { Name = "We are all your base" };
NodeReference<AllYourBase> allYourBaseRef = client.Create(allYourBase);
Console.WriteLine("AllYourBase node.id: {0}",allYourBaseRef.Id);
var queryAllYourBase = client.Cypher.Start("n", "node(" + allYourBaseRef.Id + ")").Return<Node<AllYourBase>>("n");
Console.WriteLine("AllYourBase node name: {0}\n", queryAllYourBase.Results.AsEnumerable<Node<AllYourBase>>().First().Data);
RelationshipReference areBelongToRef = client.CreateRelationship(allYourBaseRef, new AreBelongTo(usRef));
var query = client.Cypher.Start("allyourbase", "node(" + allYourBaseRef.Id + ")").Match("allyourbase-[:ARE_BELONG_TO]->us").Return<Node<AllYourBase>>("allyourbase");
query.ExecuteWithoutResults();
Console.WriteLine("Result of querying for all your base that belongs to us: {0}", query.Results.AsEnumerable<Node<AllYourBase>>().First().Data.Name);
}
catch(Exception ex)
{
Console.WriteLine("{0}", ex.Message);
Console.WriteLine("{0}", ex.InnerException);
}
Console.ReadKey();
}
}
public class Us
{
public string Name {get; set;}
public Us()
{
}
}
public class AllYourBase
{
public string Name { get; set; }
public AllYourBase()
{
}
}
public class AreBelongTo : Relationship, IRelationshipAllowingSourceNode<AllYourBase>,
IRelationshipAllowingTargetNode<Us>
{
public AreBelongTo(NodeReference targetNode)
: base(targetNode)
{}
public const string TypeKey = "ARE_BELONG_TO";
public override string RelationshipTypeKey
{
get { return TypeKey; }
}
}
you could have a look at the tests, http://hg.readify.net/neo4jclient/src/4693da483a90/Test/RelationshipTests.cs or contact the author on the Neo4j mailing list, groups.google.com/group/neo4j ?

Categories