You can use the SharePoint client object model (CSOM) to retrieve, update, and manage data in SharePoint. SharePoint makes the CSOM available in several forms and in this article I focus on .Net assemblies. SharePoint developers used to use CSOM through console application to perform CRUD operations in SharePoint on-Prem. So, they are looking for the same in SPO. The article is explaining the steps to do that.
First, you will need to register the app and give it site collection privillage so we can manage the site collection and create SPO lists, columns, libraries..etc. The steps are mentioned in SPO REST API’S AND POSTMAN to authorize the app and make sure that you copied ClientID and secret.
Second, VS 2022 will be our developer tool to create a console application and select target framework .NET7
Also, you will need to install Nuget package PnP.Framework to instantiate the client context using clientID and secret that were generated in registering and authorizing the app
Add configuration file to the console application to add the values for the authentication information in that file. So, you will add keys to the file and your configuration file should like this
<configuration>
<appSettings>
<add key="clientId" value=""/>
<add key="secretV" value=""/>
<add key="siteUrl" value=""/>
</appSettings>
</configuration>
Now, you are ready to insatiate the client context from your console application. You can create a method with return type ClientContext then you can reuse it each time instead of repeating the code. The method will read the parameter from the configuration file and will use PnP.Framework.AuthenticationManager() to instantiate the context. So, your code should look like
static ClientContext InstantiateCtx()
{
var clientId = ConfigurationManager.AppSettings["clientId"];
var secretV = ConfigurationManager.AppSettings["secretV"];
var siteUrl = ConfigurationManager.AppSettings["siteUrl"];
return new PnP.Framework.AuthenticationManager().GetACSAppOnlyContext(siteUrl, clientId, secretV);
}
Lat’s retrieve the web application information such as Title and enumerate the lists in the web application.
internal static void GetWebInfo()
{
using (var cx = InstantiateCtx())
{
cx.Load(cx.Web, p => p.Title);
cx.ExecuteQuery();
Console.WriteLine(cx.Web.Title);
};
}
internal static void GetAllLists()
{
using (var cx = InstantiateCtx())
{
Web web = cx.Web;
cx.Load(web.Lists,
lists => lists.Include(list => list.Title,
list => list.Id));
cx.ExecuteQuery();
foreach (List list in web.Lists)
{
Console.WriteLine(list.Title);
}
};
}
If you will need to create a new list, you can ListCreationInformation object to identify list type and title and description. The instantiated context will take the list information and add it to the specified web.
internal static void CreateLst()
{
using (var cx = InstantiateCtx())
{
Web web = cx.Web;
ListCreationInformation creationInfo = new ListCreationInformation();
creationInfo.Title = "CsomLst";
creationInfo.TemplateType = (int)ListTemplateType.Announcements;
List list = web.Lists.Add(creationInfo);
list.Description = "Hello from Console";
list.Update();
cx.ExecuteQuery();
Console.WriteLine("List is created ..! ");
};
}
You can use the following code segment to start adding column to the list and identify the datatype as well.
internal static void CreateLstColumn()
{
using (var cx = InstantiateCtx())
{
Web web = cx.Web;
List list = cx.Web.Lists.GetByTitle("CsomLst");
if (list != null)
{
Field field = list.Fields.AddFieldAsXml("<Field DisplayName='AnonunceNo' Type='Number' />",
true, AddFieldOptions.DefaultValue);
FieldNumber fldNumber = cx.CastTo<FieldNumber>(field);
fldNumber.MaximumValue = 100;
fldNumber.MinimumValue = 35;
fldNumber.Update();
cx.ExecuteQuery();
Console.WriteLine("List is updated by adding one column ..! ");
}
};
}
Summary:
CSOM gives flexibly to SP developers to perform many coding tasks against SharePoint Online site. You just need to authorize the app to run and then instantiate the client context. Then you are ready to perform all the CRUD operation.
Resources:
This website is known as a walk-via for the entire information you wanted about this and didn’t know who to ask. Glimpse right here, and also you’ll definitely discover it.
You actually make it appear so easy together with your presentation but I find this matter to be really something that I believe I’d never understand. It sort of feels too complex and extremely vast for me. I’m having a look forward on your next put up, I will try to get the dangle of it!