I was given a SharePoint 2010 requirement..And...Oh...I actually felt terrible to not work in SharePoint 2013, if not SharePoint 2016.But, thankfully, the below code is valid for SharePoint 2013.
Hopefully, things are better in SharePoint 2016 to read data from external domain and get the results in JSON which can be used for various purposes. A reference to SharePoint.Client and System.Web.Extentions need to be added. Below are two codes, one to convert a data to JSON and the other code to read SharePoint list and return in JSON format.
Code 1: Read data and convert to JSON
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
//References::http://stackoverflow.com/questions/6201529/turn-c-sharp-object-into-a-json-string-in-net-4
//https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
namespace MSDNJsonData
{
public class MyDate
{
public int year;
public int month;
public int day;
}
public class Lad
{
public string firstName;
public string lastName;
public MyDate dateOfBirth;
}
class Program
{
static void Main(string[] args)
{
var obj = new Lad
{
firstName = "Thangu",
lastName = "Not Applicable",
dateOfBirth = new MyDate
{
year = 2016,
month = 7,
day = 04
}
};
var json = new JavaScriptSerializer().Serialize(obj);
Console.WriteLine(json);
Console.Read();
}
}
}
Code 2: To read SharePoint and return data in JSON format.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using System.Web.Script.Serialization;
namespace ConsoleApplicationReadSPList
{
using ServiceReference1;
public class SPAnnouncement
{
public string Title;
}
public class SPEmailGroup
{
public string Email;
public string MailGroup;//A lookup column
}
class Program
{
static void Main(string[] args)
{
string websiteUrl = http://xxx.sharepoint.com;
ServiceReference1.XXX context = new ServiceReference1.XXX(new Uri(websiteUrl + "/_vti_bin/listdata.svc"));
context.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Starting with ClientContext, the constructor requires a URL to the
// server running SharePoint.
ClientContext clientcontext = new ClientContext(websiteUrl);
// Assume the web has a list named "Announcements".
List announcementsList = clientcontext.Web.Lists.GetByTitle("Announcements");
// This creates a CamlQuery that has a RowLimit of 100, and also specifies Scope="RecursiveAll"
// so that it grabs all list items, regardless of the folder they are in.
CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
ListItemCollection items = announcementsList.GetItems(query);
// Retrieve all items in the ListItemCollection from List.GetItems(Query).
clientcontext.Load(items);
clientcontext.ExecuteQuery();
foreach (ListItem listItem in items)
{
// We have all the list item data. For example, Title.
Console.WriteLine(listItem["Title"]);
var obj = new SPAnnouncement
{
Title = listItem["Title"].ToString()
};
var json = new JavaScriptSerializer().Serialize(obj);
Console.WriteLine(json);
}
//Reading another list with two values
// Assume the web has a list named "Announcements".
List emailList = clientcontext.Web.Lists.GetByTitle("EmailGroup");
// This creates a CamlQuery that has a RowLimit of 100, and also specifies Scope="RecursiveAll"
// so that it grabs all list items, regardless of the folder they are in.
CamlQuery query2 = CamlQuery.CreateAllItemsQuery(100);
ListItemCollection items2 = emailList.GetItems(query);
// Retrieve all items in the ListItemCollection from List.GetItems(Query).
clientcontext.Load(items2);
clientcontext.ExecuteQuery();
foreach (ListItem listItem2 in items2)
{
// We have all the list item data. For example, Title.
Console.WriteLine(listItem2["Email"]);
var mail = listItem2["MailGroup"] as FieldLookupValue;
var obj2 = new SPEmailGroup
{
Email = listItem2["Email"].ToString(),
MailGroup = mail.LookupValue
};
var json2 = new JavaScriptSerializer().Serialize(obj2);
Console.WriteLine(json2);
}
Console.Read();
}
}
}
Hopefully, things are better in SharePoint 2016 to read data from external domain and get the results in JSON which can be used for various purposes. A reference to SharePoint.Client and System.Web.Extentions need to be added. Below are two codes, one to convert a data to JSON and the other code to read SharePoint list and return in JSON format.
Code 1: Read data and convert to JSON
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
//References::http://stackoverflow.com/questions/6201529/turn-c-sharp-object-into-a-json-string-in-net-4
//https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx
namespace MSDNJsonData
{
public class MyDate
{
public int year;
public int month;
public int day;
}
public class Lad
{
public string firstName;
public string lastName;
public MyDate dateOfBirth;
}
class Program
{
static void Main(string[] args)
{
var obj = new Lad
{
firstName = "Thangu",
lastName = "Not Applicable",
dateOfBirth = new MyDate
{
year = 2016,
month = 7,
day = 04
}
};
var json = new JavaScriptSerializer().Serialize(obj);
Console.WriteLine(json);
Console.Read();
}
}
}
Code 2: To read SharePoint and return data in JSON format.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using System.Web.Script.Serialization;
namespace ConsoleApplicationReadSPList
{
using ServiceReference1;
public class SPAnnouncement
{
public string Title;
}
public class SPEmailGroup
{
public string Email;
public string MailGroup;//A lookup column
}
class Program
{
static void Main(string[] args)
{
string websiteUrl = http://xxx.sharepoint.com;
ServiceReference1.XXX context = new ServiceReference1.XXX(new Uri(websiteUrl + "/_vti_bin/listdata.svc"));
context.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Starting with ClientContext, the constructor requires a URL to the
// server running SharePoint.
ClientContext clientcontext = new ClientContext(websiteUrl);
// Assume the web has a list named "Announcements".
List announcementsList = clientcontext.Web.Lists.GetByTitle("Announcements");
// This creates a CamlQuery that has a RowLimit of 100, and also specifies Scope="RecursiveAll"
// so that it grabs all list items, regardless of the folder they are in.
CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
ListItemCollection items = announcementsList.GetItems(query);
// Retrieve all items in the ListItemCollection from List.GetItems(Query).
clientcontext.Load(items);
clientcontext.ExecuteQuery();
foreach (ListItem listItem in items)
{
// We have all the list item data. For example, Title.
Console.WriteLine(listItem["Title"]);
var obj = new SPAnnouncement
{
Title = listItem["Title"].ToString()
};
var json = new JavaScriptSerializer().Serialize(obj);
Console.WriteLine(json);
}
//Reading another list with two values
// Assume the web has a list named "Announcements".
List emailList = clientcontext.Web.Lists.GetByTitle("EmailGroup");
// This creates a CamlQuery that has a RowLimit of 100, and also specifies Scope="RecursiveAll"
// so that it grabs all list items, regardless of the folder they are in.
CamlQuery query2 = CamlQuery.CreateAllItemsQuery(100);
ListItemCollection items2 = emailList.GetItems(query);
// Retrieve all items in the ListItemCollection from List.GetItems(Query).
clientcontext.Load(items2);
clientcontext.ExecuteQuery();
foreach (ListItem listItem2 in items2)
{
// We have all the list item data. For example, Title.
Console.WriteLine(listItem2["Email"]);
var mail = listItem2["MailGroup"] as FieldLookupValue;
var obj2 = new SPEmailGroup
{
Email = listItem2["Email"].ToString(),
MailGroup = mail.LookupValue
};
var json2 = new JavaScriptSerializer().Serialize(obj2);
Console.WriteLine(json2);
}
Console.Read();
}
}
}