Trying to get all records of a generic list using Sharepoint and Microsoft Graph api - sharepoint

When trying to get records of a list in SharePoint i keep getting Response like:
{"#odata.context":"https://graph.microsoft.com/beta/$metadata#users('123')/sharepoint/sites('456')/lists('789')/items","value":[]}
I was able to run through all sites and lists but still fail on items. The list has the GenericList template. However on another list with the template DesignCatalog i were able to get all items. Is "/items" the wrong way to get records of a generic list?
Here is a snippet of my current Code:
const string serviceEndpoint = "https://graph.microsoft.com/beta/";
HttpClient client = new HttpClient();
var token = await _authenticationHelper.GetTokenAsync();
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
// get the site
HttpResponseMessage responseGetSites = await client.GetAsync(new Uri(serviceEndpoint + "sharePoint:/Intranet"));
if (responseGetSites.IsSuccessStatusCode)
{
string responseContent = await responseGetSites.Content.ReadAsStringAsync();
var jResult = JObject.Parse(responseContent);
siteItem = JsonConvert.DeserializeObject<SiteItemModel>(jResult.ToString());
// get all lists with the given site id
HttpResponseMessage responseGetLists = await client.GetAsync(new Uri(serviceEndpoint + "sharepoint/sites/" + siteItem.SiteId + "/lists"));
if (responseGetLists.IsSuccessStatusCode)
{
string responseContent2 = await responseGetLists.Content.ReadAsStringAsync();
var jResult2 = JObject.Parse(responseContent2);
foreach (JObject listresponse in jResult2["value"])
{
ListItemModel desiralizedItemModel = JsonConvert.DeserializeObject<ListItemModel>(listresponse.ToString());
listItemCollection.Add(desiralizedItemModel);
}
// find a specific list
string listId = listItemCollection.Where(w => w.listName == "MyTestlist").First().listId;
// get all records with of the given list
HttpResponseMessage responseGetItems = await client.GetAsync(new Uri(serviceEndpoint + "sharepoint/sites/" + siteItem.SiteId + "/lists/" + listId + "/items"));
if (responseGetItems.IsSuccessStatusCode)
{
string responseContent3 = await responseGetItems.Content.ReadAsStringAsync();
var jResult3 = JObject.Parse(responseContent3);

I had the same problem. I had to add "Sites.ReadWrite.All" permission under MS Graph, not under sharepoint (I was writing to the list as well, but "Sites.Read.All" should work).

Related

Create Purchase Receipt from REST API

I'm trying to use the new OpenAPI 2.0 setup in Acumatia 21R1 to create a PO Receipt. I've tested creating several other types of records (eg: StockItems) and virtually the same code works perfectly. This instance of Acumatica has several tenants but no branches.
I've also used this same code put pointed it to an instance of Acumatica that has one tenant and multiple branches, and as long as I set the branch it works fine.
When I run it I get "curyid can not be null". This leads me to believe that somehow it's not figuring out the branch or tenant correctly, but if I use the same login code I can pull data from those tenants just fine. I've also tried passing the json directly and including the curyid set to USD, but that has the same effect.
Any thoughts on what I am doing wrong?
private createPOR()
{
string baseURL = "https://acumatica.[good url goes here].com/AcumaticaPWS";
string strLogin = baseURL + "/entity/auth/login";
var client = new RestClient(strLogin);
var request = new RestRequest(Method.POST);
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/json");
string strBody = "{ \"name\": \"admin\", \"password\": \"[proper password]\", \"tenant\": \"PWS Milwaukee\", \"branch\": \"\"}";
request.AddJsonBody(strBody);
IRestResponse response = client.Execute(request);
txtResults.Text = response.Cookies.SingleOrDefault(x => x.Name == ".ASPXAUTH").Value.ToString();
RestResponseCookie sessionCookie = response.Cookies.SingleOrDefault(x => x.Name == ".ASPXAUTH");
// Create the new one for send the data.
string strItems = baseURL + "/entity/Default/20.200.001/PurchaseReceipt";
client = new RestClient(strItems);
request = new RestRequest(Method.PUT);
request.AddCookie(sessionCookie.Name, sessionCookie.Value);
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/json");
PurchaseReceipt theRec = new PurchaseReceipt{ Type = new StringValue { Value = "Receipt" },
VendorID = new StringValue { Value = "V1002" }
};
// BaseCurrencyID = new StringValue { Value = "USD" },
// CurrencyID = new StringValue { Value = "USD" }
request.AddJsonBody(JsonConvert.SerializeObject(theRec));
response = client.Execute(request);
txtResults.Text += Environment.NewLine + "Response" + response.StatusCode.ToString();
// Setup to log out.
string strLogout = baseURL + "/entity/auth/logout";
client = new RestClient(strLogout);
request = new RestRequest(Method.POST);
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/json");
response = client.Execute(request);
txtResults.Text += Environment.NewLine + response.StatusCode.ToString();
}
Like Samvel mentioned in the comments this issue is usually the result of not having provided the Branch during the login.
But once the Branch has been provided it still needs to be provided as a cookie for the other request down the line.
That cookie should have the name "UserBranch" with its value being an integer that should be returned from the login request.

How to get groups that user belong to?

Using Graph Service, I have tried to get list of groups that user belong to. Here is my sample code:
var userMemberOf = null;
var userMemberGroups = null;
const GraphService = require('graph-service');
const ClientCredentials = require('client-credentials');
const tenant = 'my-company.com';
const clientId = '0b13aa29-ca6b-42e8-a083-89e5bccdf141';
const clientSecret = 'lsl2isRe99Flsj32elwe89234ljhasd8239jsad2sl=';
const credentials = new ClientCredentials(tenant, clientId, clientSecret);
const service = new GraphService(credentials);
service.get('/users/tnguyen482#my-company.com/memberOf').then(response => {
userMemberOf = response.data;
});
var settings = {
"securityEnabledOnly": true
}
service.post('/users/tnguyen482#my-company.com/getMemberGroups', settings).then(response => {
userMemberGroups = response.data;
});
The data return from both get & post method was an empty list. I have tried another user id but the result is the same. Am I correct when using method memberOf and getMemberGroups to get list of groups that user belong to? Does my sample code correct?
When the user is in no group graph will return an empty result.
Otherwise, when an error occured (e.g. accessDenied or user not found) an corresponding http-status with an errormessage will be returned (more information in the documentation).
The operations you are using seem to be correct.
If you want to rule out that your operations/code is incorrect, you should try executing the operations in the Graph Explorer.
Its a great tool for debugging and you can even login for access to your own data.

Application breaks while View Redirect to Azure login

I have a mvc application , Which have a Home view , in the Home View I have Button to see the report , Which is loading two partial view one is get the ajax call and load the external api data from table ,another one partial view is for load powerbi.
while click the button, i have load different api and get the datas in 1 partial view and while loading the second partial view for PowerBi , which redirect to azure login and breaks the application.
public ActionResult Index(){
var #params = new NameValueCollection
{
{"response_type", "code"},
{"client_id", Properties.Settings.Default.ClientID},
{"resource", "https://analysis.windows.net/powerbi/api"},
{"redirect_uri", "http://localhost:13526/Redirect"}
};
var queryString = HttpUtility.ParseQueryString(string.Empty);
queryString.Add(#params);
string authorityUri = "https://login.windows.net/common/oauth2/authorize/";
var authUri = String.Format("{0}?{1}", authorityUri, queryString);
Response.Redirect(authUri);
}
After Authentication ,Azure issues the token and load the powerbi.
EmbedPowerBi
public async Task<ActionResult> EmbedPowerBi ()
{
AuthenticationResult authResult;
authResult = (AuthenticationResult)Session["authResult"];
var token = authResult.AccessToken;
ViewBag.Token = token;
var tokenCredentials = new TokenCredentials(token, "Bearer");
// Create a Power BI Client object. It will be used to call Power BI APIs.
using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
{
// Get a list of dashboards.
var dashboards = await client.Dashboards.GetDashboardsInGroupAsync(GroupId);
// Get the first report in the group.
var dashboard = dashboards.Value.FirstOrDefault();
//var dashboard = dashboards.Value.Where(w => w.Id == "DashboardId");
// Generate Embed Token.
var generateTokenRequestParameters = new GenerateTokenRequest(accessLevel: "view");
var tokenResponse = await client.Dashboards.GenerateTokenInGroupAsync(GroupId, dashboard.Id, generateTokenRequestParameters);
if (tokenResponse == null)
{
return View(new EmbedConfig()
{
ErrorMessage = "Failed to generate embed token."
});
}
// Generate Embed Configuration.
var embedConfig = new EmbedConfig()
{
EmbedToken = tokenResponse,
EmbedUrl = dashboard.EmbedUrl,
Id = dashboard.Id
};
return View(embedConfig);
}
}
Problem I have faced beacuse of redirection other partial view gets affected the current application.
In the Home View i am loading partial view.
#Html.Partial("loadProduct");
#Html.Partial("_EmbedPowerBi");
Have you tried referring to the existing 'Developer Samples'?
https://github.com/Microsoft/PowerBI-Developer-Samples
https://github.com/Microsoft/PowerBI-Developer-Samples/blob/master/App%20Owns%20Data/PowerBIEmbedded_AppOwnsData/Controllers/HomeController.cs#L133
It seems the redirection problem is causing a ViewModel binding to you EmbedConfig

oAuth2 web request works in browser but not in app

I have the following code sample with which I'm trying to authenticate an Azure active directory user within a Xamarin forms app
The URL (I've removed the actual client ID) works fine in a browser but fails when trying to send the http request
The error message says 'the response type must include client_id'
string URL = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?"
+ "client_id=xxxx-xxxxx-xxxxx-xxxxx-xxx"
+ "&response_type=code"
+ "&redirect_uri=https://login.microsoftonline.com/common/oauth2/nativeclient"
+ "&response_mode=query"
+ "&scope=openid%20offline_access%20https%3A%2F%2Fgraph.microsoft.com%2Fmail.read"
+ "&state=12345";
var webRequest = System.Net.WebRequest.Create(URL) as HttpWebRequest;
System.Console.WriteLine(URL);
if (webRequest != null)
{
webRequest.Method = "POST";
webRequest.ServicePoint.Expect100Continue = false;
webRequest.Timeout = 20000;
webRequest.ContentType = "text/html";
//POST the data.
using (requestWriter = new StreamWriter(webRequest.GetRequestStream()))
{
requestWriter.Write(postData);
}
}
HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();
Stream resStream = resp.GetResponseStream();
StreamReader reader = new StreamReader(resStream);
ret = reader.ReadToEnd();
You put parameters in the URL, so you need to use GET method, instead of POST (like your browser does when you paste the URL in its address bar).
So, replace:
webRequest.Method = "POST";
by:
webRequest.Method = "GET";
and remove:
//POST the data.
using (requestWriter = new StreamWriter(webRequest.GetRequestStream()))
{
requestWriter.Write(postData);
}

Documentdb Failed to deserialize stored procedure response or convert it to my defined type

My Stored Procedure: (I created it via Azure Script Explorer)
function GetAllResources() {
var collection = getContext().getCollection();
// Query documents and take 1st item.
var isAccepted = collection.queryDocuments(
collection.getSelfLink(),
'SELECT * FROM MultiLanguage as m',
function (err, docs, options) {
if (err) throw err;
// Check the feed and if empty, set the body to 'no docs found',
// else take 1st element from feed
if (!docs || !docs.length) getContext().getResponse().setBody('no docs found');
else getContext().getResponse().setBody(JSON.stringify(docs));
});
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
The sproc can be executed successfully from script explorer.
My C# code to call the sproc:
public async Task<IHttpActionResult> GetReources() {
client = new DocumentClient(new Uri(ConfigurationManager.AppSettings["endpoint"]), ConfigurationManager.AppSettings["authKey"]);
var collectionLink = UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId);
//var docs = await client.ReadDocumentFeedAsync(collectionLink, new FeedOptions { MaxItemCount = 10 });
//var docs = from d in client.CreateDocumentQuery<Models.Resource>(collectionLink)
// select d;
StoredProcedure storedProcedure = client.CreateStoredProcedureQuery(collectionLink).Where(c => c.Id == "GetAllResources").AsEnumerable().FirstOrDefault();
Models.Resource docs = await client.ExecuteStoredProcedureAsync<Models.Resource>(storedProcedure.SelfLink);
foreach (var d in docs) {
Models.Resource a = new Models.Resource();
a = docs;
//a.id = d.id;
//a.Scenario = d.Scenario;
//a.Translations = d.Translations;
//a.LastModified = d.LastModified;
//a.ModifiedBy = d.ModifiedBy;
//a.LastAccessed = d.LastAccessed;
resources.Add(a);
}
return Ok(resources);
}
First, there is an error for the "foreach..." like said
foreach cannot operate on variables of type Models.Resource because it
doesn't contain a public definition of GetEnumerator.
Then I tried to modify my sproc to only return 1 result and remove the foreach line, then I got error said
Failed to deserialize stored procedure response or convert it to type
'Models.Resource'
I just want to return the result of the stored procedure as my defined class (Models.Resource). How to do this?
It can be simpler to get sproc by name using CreateStoredProcedureUri, like this:
const string endpoint = "https://your.service.azure.com:443/";
const string authKey = "<your magic secret master key>==";
var client = new DocumentClient(new Uri(endpoint), authKey);
Uri sprocUri = UriFactory.CreateStoredProcedureUri("databaseName", "collectionName", "GetAllResources");
var result = await client.ExecuteStoredProcedureAsync<string>(sprocUri);
The stored procedure above serializes results of the query (docs array) to string, if you keep it this way, the result of sproc would be string, which I guess you would need to manually deserialize to objects. You can do this simpler, just return docs from sproc and have result as objects (like Models.Resource[]), serialization would happen automatically.
If you change the sproc to return just one doc (e.g. do __.response.setBody(docs[0]) and Models.Resource represent one item, then the call is correct:
Models.Resource doc = await client.ExecuteStoredProcedureAsync<Models.Resource>(sprocUri);
Also, to // Query documents and take 1st item, I wouldn't recommend to use script as script has overhead of running JavsScript engine. Scripts kick in when you have bulk operations (to optimize for network traffic) or have business logic which makes sense to run on the server. To take 1st item you can do query from client like this: SELECT TOP 1 * FROM c. Typically you would WHERE and ORDER BY clause to that.
There is a number of docdb samples on github, for instance, https://github.com/Azure/azure-documentdb-dotnet/tree/master/samples/code-samples/ServerSideScripts and https://github.com/Azure/azure-documentdb-dotnet/tree/master/samples/code-samples/Queries.
Thanks,
Michael
All right, let's make sure we are on the same page.
I am using the sproc same as above.
I am using client code like this:
class Models
{
// This would have more properties, I am just using id which all docs would have.
public class Resource
{
[JsonProperty("id")]
public string Id { get; set; }
}
}
public async Task<IHttpActionResult> GetResources()
{
const string endpoint = "https://myservice.azure.com:443/";
const string authKey = "my secret key==";
var client = new DocumentClient(new Uri(endpoint), authKey);
Uri sprocUri = UriFactory.CreateStoredProcedureUri("db", "c1", "GetAllResources");
var serializedDocs = await client.ExecuteStoredProcedureAsync<string>(sprocUri);
Models.Resource[] resources = JsonConvert.DeserializeObject<Models.Resource[]>(serializedDocs);
return Ok(resources);
}
It works fine. Is this what you are doing?

Resources