GraphServiceClient, Create ContentVersions to intune with GraphServiceClient - c#

I'm having trouble creating a new ContentVersion - Right now I have created a new entry with this code.
var newItem = await client.DeviceAppManagement.MobileApps.Request().AddAsync(new Win32LobApp {});
But I can't find the method to create a new ContentVersion. I know the URL, but not the method to call.
The URL to call is this
POST: https://graph.microsoft.com/beta/deviceAppManagement/mobileApps/{0}/microsoft.graph.win32LobApp/contentVersions
So far I have tried to clone the github project (https://github.com/microsoftgraph/msgraph-beta-sdk-dotnet) to see how it works, but without luck. And right now, I just don't know where to look.
Any help would be appreciated

Okay, I solved it by doing it like this
ManagedMobileLobAppRequestBuilder builder = new ManagedMobileLobAppRequestBuilder($"{client.BaseUrl}/deviceAppManagement/mobileApps/{newItem.Id}/{newItem.ODataType.Substring(1)}", client);
var result = await builder.ContentVersions.Request().AddAsync(new MobileAppContent());
Not sure if this is the correct way of doing it, but it works!

An alternative solution with a little less string concatenation:
var lobType = createAppResult.ODataType.Substring(1);
var baseUrl = client.DeviceAppManagement.MobileApps[appId].AppendSegmentToRequestUrl(lobType)
var lobRequestBuilder = new ManagedMobileLobAppRequestBuilder(baseUrl, client);

Related

How to handle VSTS credentials in a VSIX extension

I have a Visual Studio extension that we use internally for a project and one of the things it needs to be able to do is post tickets to VSTS. Previously we were using onsite TFS and making a connection to post tickets was as simple as:
var vssCreds = new VssCredentials(true);
projectCollection = new TfsTeamProjectCollection(url, vssCreds);
workItems = projectCollection.GetService<WorkItemStore>();
project = workItems.Projects["My Project"];
defaultType = project.WorkItemTypes["Bug"];
//...
var newItem = new WorkItem(defaultType)
{
Title = title
};
newItem.Fields["Assigned To"].Value = assignTo;
newItem.Fields["Repro Steps"].Value = repoSteps;
var validationResult = newItem.Validate();
newItem.Save();
And this worked fine. But after upgrading to VSTS I'm having a hard time getting the credentials part to work. I changed this line:
projectCollection = new TfsTeamProjectCollection(url, vssCreds);
To this:
projectCollection = new TfsTeamProjectCollection(url, new VssClientCredentials());
And this worked fine for me. But when I share it with other people on my team it didn't work at first and then started working a little later. I am guessing that interacting with VSTS caused their credentials to be loaded so that it then worked. But I have at least one person who just seems to be completely unable to make it work.
So what's the correct way to get it to use the VSTS credentials (that should already exist in VS)?
I see this overload for VssClientCredentials (https://msdn.microsoft.com/en-us/library/dn228355(v=vs.120).aspx):
public VssClientCredentials(
IVssCredentialPrompt credentialPrompt
)
Which I suspect might be useful, but I can't seem to find out if there's a built in implementation of IVssCredentialPrompt somewhere or, if not, how to implement it.
Remove the related key from
Computer\HKEY_CURRENT_USER\Software\Microsoft\VSCommon\14.0\ClientServices\TokenStorage\VisualStudio\VssApp, then authentication again.
You also can specify other Kind (vssApp by default) and Namespace (VisualStudio by default) by using this code:
var c = new VssClientCredentials();
c.Storage = new VssClientCredentialStorage(storageKind: "VssApp2", storageNamespace: "VisualStudio");
projectCollection = new TfsTeamProjectCollection(url, c);
For reasons that are entirely unclear and from this answer to an entirely different question: https://stackoverflow.com/a/40256731/1250301
It seems that spawning a new thread causes a login prompt to appear if needed and fix all the problems. So if I do this:
Task.Run(() =>
{
var url = new Uri(_tfsUrl);
var cred = new VssClientCredentials();
projectCollection = new TfsTeamProjectCollection(url, cred);
workItems = projectCollection.GetService<WorkItemStore>();
}).Wait();
project = workItems.Projects["Job Posting Data"];
defaultType = project.WorkItemTypes["Bug"];
taskType = project.WorkItemTypes["Task"];
Then it works. I have no idea why it works, or why this is necessary (at first I thought maybe it was a problem with not being in the UI thread so I'd tried Application.Current.Dispatcher.Invoke which did not work) but it seems to have fixed the problem.

How to use Orc.SystemInfo?

I downloaded via nuget the package Orc.SystemInfo (Orc.SystemInfo). I managed to display some SystemInfoElements as a list. However no name is provided (name in List is null). I think there is a problem with the LanguageService. I already googled for this problem but I was unable to find a solution.
Here is my code so far (Linq-Pad)
void Main()
{
var service = new Orc.SystemInfo.WindowsManagementInformationService();
var dotnet = new Orc.SystemInfo.DotNetFrameworkService();
var db = new Orc.SystemInfo.DbProvidersService();
ILanguageService lang = new Catel.Services.LanguageService();
var info = new Orc.SystemInfo.SystemInfoService(service, dotnet, lang, db);
var infoList = info.GetSystemInfo();
infoList.Dump();
}
Thank you for your help!
You can simply resolve the ISystemInfo service using the ServiceLocator and all should be taken care of for you.

mongodb gridfs in C# new api

i am new with MongoDB, and i can't find GridFS.
Where can i get GridFS to store files now ?
I can get it this way:
mongoClient = new MongoClient(Settings.Default.MongoDB);
var server = mongoClient.GetServer();
MongoDatabase = server.GetDatabase(Settings.Default.DatabaseName);
MongoDatabase.GridFS...
but GetServer() method is obsolete.
if I get database as here:
MongoDatabase2 = mongoClient.GetDatabase(Settings.Default.DatabaseName);
MongoDatabase2.GridFS... not working
Then i receive IMongoDatabase instead MongoDatabase, and i didnt have GridFS.
Heureka!! Got it! Give me the Nobel prize! :)
var grid = new MongoGridFS(new MongoServer(new MongoServerSettings {Server = new MongoServerAddress(host, port)}), databaseName, new MongoGridFSSettings());
grid.Upload(file.InputStream, file.FileName, new MongoGridFSCreateOptions
{
Id = imageId,
ContentType = file.ContentType
});
I have the same problem. I did find something I thought would solve my problem, the class MongoGridFS. My problem now is that it takes a MongoServer, as it's first arg and times out when I give it a new instance or complains about a connectionstring not found.
var grid = new MongoGridFS(new MongoServer(new MongoServerSettings { Server = new MongoServerAddress(Settings.Default.WizdooMongoConnectionString) }), Settings.Default.WizdooMongoDatabaseName, new MongoGridFSSettings());
grid.Upload(file.InputStream, file.FileName, new MongoGridFSCreateOptions
{
Id = imageId,
ContentType = file.ContentType
});
//Yealds: The settings property 'ConnectionString' was not found.
I suppose I need to give it a connectionstring, but I've been able to do CRUD fine in MongoDB without it. So weird for this service to require it all of a sudden. Maybe you can make it work for you though. I'm going to look in to it more when I have time... should work with correct config. If you get it to work pls give me a hint! Good luck! :)

How to create indexes in MongoDB via .NET

I've programmatically created a new document collection using the MongoDB C# driver.
At this point I want to create and build indexes programmatically. How can I do that?
Starting from v2.0 of the driver there's a new async-only API. The old API should no longer be used as it's a blocking facade over the new API and is deprecated.
The currently recommended way to create an index is by calling and awaiting CreateOneAsync with an IndexKeysDefinition you get by using Builders.IndexKeys:
static async Task CreateIndexAsync()
{
var client = new MongoClient();
var database = client.GetDatabase("HamsterSchool");
var collection = database.GetCollection<Hamster>("Hamsters");
var indexKeysDefinition = Builders<Hamster>.IndexKeys.Ascending(hamster => hamster.Name);
await collection.Indexes.CreateOneAsync(new CreateIndexModel<Hamster>(indexKeysDefinition));
}
you should use CreateIndex as EnsureIndex is marked obsolete for future compatibility with the next versions of MongoDB:
var client = new MongoClient("mongodb://localhost");
var db = client.GetServer().GetDatabase("db");
var collection = db.GetCollection<Hamster>("Hamsters");
collection.CreateIndex(IndexKeys<Hamster>.Ascending(_ => _.Name));
The overload of CreateOneAsync in the currently accepted answer is now marked as obsolete with the message "Use CreateOneAsync with a CreateIndexModel instead." Here's how you do it:
static async Task CreateIndex(string connectionString)
{
var client = new MongoClient(connectionString);
var database = client.GetDatabase("HamsterSchool");
var collection = database.GetCollection<Hamster>("Hamsters");
var indexOptions = new CreateIndexOptions();
var indexKeys = Builders<Hamster>.IndexKeys.Ascending(hamster => hamster.Name);
var indexModel = new CreateIndexModel<Hamster>(indexKeys, indexOptions);
await collection.Indexes.CreateOneAsync(indexModel);
}
Something like this should do:
var server = MongoServer.Create("mongodb://localhost");
var db = server.GetDatabase("myapp");
var users = db.GetCollection<User>("users");
users.EnsureIndex(new IndexKeysBuilder().Ascending("EmailAddress"));
Please see the following bits in the documentation:
http://api.mongodb.org/csharp/current/html/06bcd201-8844-3df5-d170-15f2b423675c.htm
There is an entire area on Indexing under the Definitions and Builders documentation page:
http://mongodb.github.io/mongo-csharp-driver/2.4/reference/driver/definitions/#index-keys
Example:
IndexKeysDefinition<MyModel> keys = "{ Reference: 1 }";
var indexModel = new CreateIndexModel<MyModel>(keys);
await _context.Indexes.CreateOneAsync(indexModel);
the easiest way to create indexes in c# is by using the driver wrapper library MongoDB.Entities. here's an example of creating a text index:
DB.Index<Author>()
.Key(a => a.Name, Type.Text)
.Key(a => a.Surname, Type.Text)
.Create();
and to do a full-text search, you simply do:
DB.SearchText<Author>("search term");
haven't seen anything else that makes it simpler than that.

XML RPC wordpress Anonymous Comments Posting

i am posting comments on my blog it works fine but there is one problem that i cannot post anonymous comments using XML-RPC by alexjamesbrown
instead of comment author there is name of admin
here is my code
var wrapper = new WordPressWrapper(textblogUrltemp + "/xmlrpc.php", adminunUser, adminPass);
var post = new Post();
post.title = toPost.VideoTitle;
post.dateCreated = Convert.ToDateTime(toPost.PostingDateTime);
post.description = toPost.VideoCode;
post.postid = wrapper.NewPost(post, true);
var comment = new Comment();
var wrap =new WordPressWrapper(textblogUrltemp + "/xmlrpc.php", adminunUser, adminPass);
comment.author = videoComments.userName;
comment.author_email = videoComments.email;
comment.content = videoComments.Comment;
wrap.NewComment(post.postid, comment);
I resolved it
var wrap =new WordPressWrapper(textblogUrltemp + "/xmlrpc.php", "", "");
also you need to modify the class-wp-xmlrpc-server.php file and in newComment function allow the anonymous comments
I bumped into this working for WP Remote Control. Very bad idea to unlock anonymous posting through XMLRPC. You can get flooded by Spam as anyone can do it.
The secret is to post the comment and edit it. Two requests but you're safe. There's a single method that does it in my class. Regards.
BTW: My class is PHP. I have it written in C++ too but it's too dependent to my C++ library to publish.

Categories