Introduction:
SharePoint List contains important information and when SharePoint List View items count goes above its threshold value, archiving the old items becomes important. In this article we will learn how to archive the SharePoint List Items to SQL Table using CSOM.
Scenario:
When a SharePoint List View goes beyond its threshold limit, you want to keep the old records as well as make sure you do not cross the SharePoint List threshold. To overcome list view threshold we can create a list view which narrow the results under threshold value. But what if the List Item count is more than 50 thousand, your SharePoint content database grows bigger with the every new item added.
In this article we are going to archive the list items in SQL table outside of SharePoint database.
Prerequisite:
- SQL Server
- SharePoint with more than its threshold limit
Solution:
The following are the key things to be done:
- Create a List view which will have the items to be archived.
- Create the SQL Table based on the List Schema.
- Perform the Archive Job using CSOM.
The purpose of creating List view is to avoid the CamlQuery for filtering the List Items. We can just get the View Query and pull the data from List.
Now this one is tricky, creating the SQL table based on list Schema. By default SharePoint creates default read-only hidden fields other than the custom fields we add. For example: Created By and Modified By are useful. But there are many other columns which are not usable for this application. You can filter what columns you want to have in yourTABLE.
Below code snippet reads the List Schema and generate the Create Table Script:
- public StringBuilder ReadListSchema(List list)
- {
- StringBuilder script = new StringBuilder();
- var listFieldCollection = list.Fields;
- _context.Load(listFieldCollection);
- _context.ExecuteQuery();
- script.AppendFormat("CREATE TABLE [{0}] (", list.Title);
-
- foreach(var field in listFieldCollection)
- {
- if (FilterColumn(field))
- CreateScript(field.Title.Replace(' ', '_'), field.FieldTypeKind, script);
-
-
- }
- script.Append("[Created_By] Varchar(MAX), [Modified_By] Varchar(MAX), [Modified] Varchar(MAX), [Created] Varchar(MAX)");
- script.Append(");");
- return script;
- }
-
- private void CreateScript(string colName, FieldType colType, StringBuilder script)
- {
- switch (colType)
- {
- case FieldType.Boolean:
- script.AppendFormat("[{0}] {1},", colName, "BIT");
- break;
- case FieldType.DateTime:
- script.AppendFormat("[{0}] {1},", colName, "DATETIME");
- break;
- caseFieldType.Integer:
- script.AppendFormat("[{0}] {1},", colName, "Int");
- break;
- case FieldType.Lookup:
- script.AppendFormat("[{0}] {1},", colName, "Varchar(MAX)");
- break;
- case FieldType.Attachments:
- script.AppendFormat("[{0}] {1},", colName, "Varchar(MAX)");
- break;
- caseFieldType.Number:
- script.AppendFormat("[{0}] {1},", colName, "Varchar(MAX)");
- break;
- default:
- script.AppendFormat("[{0}] {1},", colName, "Varchar(MAX)");
- break;
- }
- }
- private bool FilterColumn(Field field)
- {
- if (!field.SchemaXml.Contains("BdcField"))
- {
- if (field.FieldTypeKind == FieldType.Computed || field.Hidden || field.Sealed)
- {
- return false;
- }
- elseif(field.ReadOnlyField && field.FieldTypeKind != FieldType.Lookup)
- {
- return false;
- }
- elseif(!field.CanBeDeleted && field.InternalName != "Attachments")
- {
- return false;
- }
- return true;
- } else
- {
- returntrue;
- }
- }
ReadListSchema Method takes the List to be archived as a parameter and it returns the script to create the SQL Table. Here, I am loading all the Fields in the List then looping each field to create the script.
There is a FilterColumn method which filters the unwanted SharePoint generated columns. And then the CreateScript method just appends the string Builder object.
Note: I will continue with the rest of the application in the next part of this article. Stay tuned.
Read more articles on SharePoint: