12 May, 2014

Download file from OneDrive: Solved

I’m lost for words with the lack of documentation for using OneDrive from a Windows Phone 8.1 application. I has literally taken me days to get this one to work! Finally, a comment on a forum somewhere guided me in the right direction.

How do you download the content of a file found on your OneDrive? If you just use the poor samples at MS webpage, you gain the metadata of the file you are downloading. Not the content which is what you are after (or I was!).

Downloading a file (content!) from a OneDrive folder is done in the following way.

1) Establish a session with OneDrive

 public async Task<LiveConnectSession> Initialize()
{
LiveAuthClient auth = new LiveAuthClient();
var r = await auth.InitializeAsync(new string[] { "wl.skydrive_update"});
return r.Session;
}
2) Download the file (important is the “/content” as id)
   public async Task<IStorageFile> LoadFile(string fileId)
{
//temporary file for write
var fileTmp = await ApplicationData.Current.LocalFolder.CreateFileAsync("tmp.xml", CreationCollisionOption.ReplaceExisting);

var session = await Initialize();
var liveClient = new LiveConnectClient(session);

//add /content to get the content of the file instead of the metadata.
await liveClient.BackgroundDownloadAsync(fileId + "/content", fileTmp);

return fileTmp;
}

3) read the file content

   public async Task RestoreFile(IStorageFile file)
{
//read incoming file
using (var fs = await file.OpenStreamForReadAsync())
{
try
{
var vault = ConvertStreamToObject(new StreamReader(fs, System.Text.Encoding.UTF8, true));

var lst = vault.Entries.OrderBy(x => x.Title).ToList();

//save to disk
await SaveDataAsync(lst);
}
catch (Exception ex)
{
TelemetryService.LogException(ex);
}
}
}

10 May, 2014

How to reset VS

My VS 2013 today completely locked up. I could not even start it; just had a spinning wheel and nothing else. It would have been nice to go into Settings – Reset or something. However – that was not possible due to the spinning wheel. Great – now what?

Luckily, Microsoft has seen this one before I reckon; as you can reset the environment from the command line:

1) Navigate to C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE

2) Run devenv.exe /resetsettings

3) Done. You can now start VS again…

image

Technorati Tags:

09 May, 2014

How to embed an image inline in an email?

Quite a new thing (to me at least), but how do you embed an image in an email programmatically? Not just attach the picture but make it appear inline as an embedded part of the html email?

Turns out that’s fairly easy (when you know how?):

MailMessage msg = new MailMessage();
foreach (var recpient in lst)
msg.To.Add(recpient);

msg.Subject = subject;
msg.IsBodyHtml =
true;
msg.Priority =
MailPriority.Normal;
msg.Body = htmlBody;

Attachment inlineLogo = new Attachment("logo.png");
msg.Attachments.Add(inlineLogo);

inlineLogo.ContentId =
"logo";
inlineLogo.ContentDisposition.Inline =
true;
inlineLogo.ContentDisposition.DispositionType =
DispositionTypeNames.Inline;

Logger.Write("Sending email to: {0}", rcpts);
new SmtpClient().Send(msg);
The accompanying html that works with this is (the important part is src=”cid:logo”)
    <p class="italic">
<
b>/Reporting System</b><br />
This email is sent out automated. You can not respond to this email.<br /><br />
<
img width="60" height="60" src="cid:logo" />
</
p>
</
body>

Technorati Tags:

iPhone/XCode - not all cases are equal!

This bit me! Having made some changes to an iPhone application (Obj-C); everything worked fine in the simulator. But, when deploying the s...