Add Geolocation field to a SharePoint Online list through code (c#)

​Small console application which allows you to add a Geolocation field to a SharePoint list (office 365)

http://sdrv.ms/17JsKzU​

Advertisements

JSON feed first DataContract empty

Your JSON feed will probably look similar to this:

[{"employees": [{ "firstName":"John" , "lastName":"Doe" },{ "firstName":"Anna" , "lastName":"Smith" },{ "firstName":"Peter" , "lastName":"Jones" }]}]

As you can see, the feed is starting with square brackets.
But when deserializing the feed you don’t get any data.
It’s because the first DataContract is empty / null.
Hereby you can’t link/map the DataMembers and won’t get any data from the feed.

Well, this can be fixed with a little workaround:

WebClient wc = new WebClient();
byte[] json = wc.DownloadData("file.json");

String newJson = Encoding.UTF8.GetString(json);
newJson = newJson.Remove(0, 1);
newJson = newJson.Remove(newJson.Length - 1, 1);

using (MemoryStream ms = 
new MemoryStream(Encoding.UTF8.GetBytes(newJson)))
{
...
}

How to pass a value from a User Control to the Page

In this example I’ll use a simple user control: WebUserControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="Controls_WebUserControl" %>
<asp:TextBox ID="TextBox1" runat="server" />

You have to make a public property to retrieve the value.

public string TextBoxValue
{
    get { return TextBox1.Text; }
} 

In the web page you have to add the reference to the user control so it’s possible to use it on the page.

<%@ Register TagPrefix="uc" TagName="Control" Src="~/Controls/WebUserControl.ascx" %>
<uc:Control ID="control" runat="server" />

Now you can easily retrieve the value from the user control by just using the public property.

string value = control.TextBoxValue;

Posted in .NET, ASP

Tags: , , ,

Permalink

Christmas Wishes

namespace Christmas
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("merry christmas");
        }
    }
}

Posted in .NET

Permalink

Generate a GUID from a String

A GUID is a 128-bit integer (16 bytes).
To generate a GUID from a string, you need to apply a hash function to the string to generate a 16 byte hash. Then pass this hash result into the Guid constructor.

In this example I used the MD5 hash function:

Guid guid;
String text = "test";
using (MD5 md5 = MD5.Create())
{
	byte[] hash = md5.ComputeHash(Encoding.Default.GetBytes(text));
	guid = new Guid(hash);
}

Generating ATOM Feed

Simple way to generate ATOM feeds.
On wikipedia you can find information about ATOM feeds and see an example of an ATOM feed. I’ll use that example as template and use the StringBuilder to create the feed.

NewsCollection NewsMessages = new NewsCollection();
NewsMessages.GetAllNewsMessages();

string location = "C:\\TEMP\\feed.xml";

string title = "Example Feed";
string subtitle = "A subtitle.";
string link = "http://example.org/";
string self = "http://example.org/feed/";
string id = "60a76c80-d399-11d9-b91C-0003939e0af6";
            
StringBuilder feed = new StringBuilder();

feed.Append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
feed.Append("<feed xmlns=\"http://www.w3.org/2005/Atom\">\n");
feed.AppendFormat("<title>{0}</title>\n", title);
feed.AppendFormat("<subtitle>{0}</subtitle>\n", subtitle);
feed.AppendFormat("<link href=\"{0}\" rel=\"self\" type=\"application/atom+xml\" />\n", self);
feed.AppendFormat("<link href=\"{0}\" />\n", link);
feed.AppendFormat("<id>urn:uuid:{0}</id>\n", id);
feed.AppendFormat("<updated>{0:s}Z</updated>\n", DateTime.Now.ToUniversalTime());

foreach (News NewsMessage in NewsMessages)
{
    feed.Append("<entry>\n\t");
    feed.AppendFormat("<title>{0}</title>\n\t", NewsMessage.Title);
    feed.AppendFormat("<link href=\"{0}\" />\n\t", NewsMessage.Link);
    feed.AppendFormat("<id>urn:uuid:{0}</id>\n\t", NewsMessage.Id);
    feed.AppendFormat("<updated>{0:s}Z</updated>\n\t", NewsMessage.PublicationDate);
    feed.AppendFormat("<summary>{0}</summary>\n\t", NewsMessage.Text);
    feed.Append("<author>\n\t\t");
    feed.AppendFormat("<name>{0}</name>\n\t\t", NewsMessage.Author.Name);
    feed.AppendFormat("<email>{0}</email>\n\t", NewsMessage.Author.Email);
    feed.Append("</author>\n");
    feed.Append("</entry>\n");
}

feed.Append("</feed>");

using (StreamWriter writer = new StreamWriter(location))
{
    writer.Write(feed.ToString());
}

Objects

public class Author
{
    public string Name { get; set; }
    public string Email { get; set; }
}

public class News
{
    public string Title { get; set; }
    public string Link { get; set; }
    public string Id { get; set; }
    public string Text { get; set; }
    public DateTime PublicationDate { get; set; }
    public Author Author { get; set; }
}

public class NewsCollection : ICollection<News>
{
    private List<News> list = new List<News>();

    public void GetAllNewsMessages()
    {
        Author author = new Author();
        author.Name = "John Doe";
        author.Email = "johndoe@example.com";

        News news1 = new News();
        news1.Author = author;
        news1.Title = "Atom-Powered Robots Run Amok";
        news1.Link = "http://example.org/2003/12/13/atom03";
        news1.Id = "1225c695-cfb8-4ebb-aaaa-80da344efa6a";
        news1.PublicationDate = new DateTime(2003, 12, 13);
        news1.Text = "Some text.";

        News news2 = new News();
        news2.Author = author;
        news2.Title = "Lorem ipsum";
        news2.Link = "http://example.org/2012/10/23/atom03";
        news2.Id = "cd52cde9-bcdd-417a-a1c5-ab6025d87bbf";
        news2.PublicationDate = new DateTime(2012, 10, 23);
        news2.Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";

        this.list.Add(news1);
        this.list.Add(news2);
    }

    #region ICollection Methods
    ...
    #endregion
}

Now after generating the ATOM feed, it’s possible to validate the feed.
http://validator.w3.org/appc/check.cgi
When validating the feed right now, you’ll get a warning message
“Self reference doesn’t match document location”.
You can ignore this message. When the feed is hosted on the same location as the self reference, this message will be gone.

Connect and use a web service in windows phone apps

This article explains how to connect to any web service and use it with a windows phone application. In my example I used this public weather SOAP web service.
For more public open web services: http://www.service-repository.com/

So first we have to add a service reference to the web service.

Now that we have our service reference, we can initialize a new client. Afterwards we can call any web method from this service client.

ServiceReference.WeatherSoapClient client = new ServiceReference.WeatherSoapClient();

All calls from windows phone applications to services are always asynchronous. So the method names will look like ‘GetMethodAsync‘. In the example we’ll use the method GetCityWeatherByZIPAsync with parameter value 18001.

client.GetCityForecastByZIPAsync("18001");

At certain time after calling the method, the data will return. At that point the method completed event will be raised and we have to react to it with an EventHandler. But this handler has to be set up before the call is made.
So you have to put this line of code before the async call.

client.GetCityWeatherByZIPCompleted += new EventHandler<ServiceReference.GetCityWeatherByZIPCompletedEventArgs>(client_GetCityWeatherByZIPCompleted);

The handler method will return the result of the service. That result is in the EventArgs variable e.

void client_GetCityWeatherByZIPCompleted(object sender, ServiceReference.GetCityWeatherByZIPCompletedEventArgs e)
{
      MessageBox.Show(String.Format("Temperature: {0} °F", e.Result.Temperature));
}