Add List Item in SharePoint Online using ASP.net and C#
In this article, I will describe how you can send data from an ASP.net (web form) application to SharePoint Online custom list.
The scenario of this request, was that a client wanted to have an internet facing (without opening its SharePoint to the public) public form. So I suggested using Windows Azure to deploy a .Net Application (ASP.net and C#) which people can access (anonymously), submit the data, and get the data sent across to a Sharepoint List.
Here are the simple steps to follow:
1- Create you form with ASP.net
I used BootStrap Framework along side to ASP.net, HTML and CSS3.
2- Download ‘SharePoint Online Client Components SDK’
3- Include the required references
You need to reference the following:
Microsoft.SharePoint.Client Microsoft.SharePoint.Client.Runtime
4- The Magic !!
Assuming that you do you proper data validation and fancy animations 🙂 (Optional), you need to run the following code in you submit event (or button).
#region Connexion to SharePoint
string WebUrl = "https://You.sharepoint.com/sites/ABC/";
string login = "**********";
string password = "**********";
var securePassword = new SecureString();
foreach (char c in password)
{securePassword.AppendChar(c);}
var onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
#endregion
#region Insert the data using (ClientContext CContext = new ClientContext(WebUrl)) { CContext.Credentials = onlineCredentials; List announcementsList = CContext.Web.Lists.GetByTitle("Supply GateWay"); ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation(); Microsoft.SharePoint.Client.ListItem newItem = announcementsList.AddItem(itemCreateInfo); newItem["Title"] = C_Name.Value; newItem["Address"] = C_Address.Value; newItem.Update(); CContext.ExecuteQuery(); }
#endregion