ArticleDirectory:
Introduction to LINQ to SharePoint
LINQ to SharePoint Application
Summary
SharePoint projects usually perform operations on lists, document libraries, websites, and other types, including site \ WEB \ list \ listitem \ Folder. SharePoint has its original query method caml, the disadvantage is that the query statement is complex and not easy to understand. The most important thing is that caml uses a string or GUID as the key for query and is prone to errors in practical applications. To address the issue of SharePoint, Microsoft introduced LINQ to Sharepoint to further simplify query and filtering in Sharepoint. However, in fact, the performance of LINQ to Sharepoint is not necessarily better than that of caml queries, because LINQ to SharePoint converts a query to a caml statement and then performs a query, naturally, there will be greater system overhead. In any case, this is a technology promoted by Microsoft. It is necessary to understand that the programming process will be simpler in scenarios where the actual data volume is not large.
Introduction to LINQ to SharePoint
I. LINQ to SharePoint
First, LINQ to SharePointProgramming LanguageC # and Microsoft Visual Basic. net. The compiler is attached to Visual Studio.
As a member of the LINQ family, if you have used such as LINQ to SQL, you will find many similarities between LINQ to SharePoint and others.
For example, there is a datacontext class in LINQ to SQL, which is used to open a database connection and perform operations. You can also use datacontext to operate data in Sharepoint.
Ii. spmetal
An important step to use LINQ to Sharepoint is to generate the entity class, that is, the SharePoint entity class, which is used to map to the document library and list corresponding to SharePoint. Manual creation of entity classes is a very painful and complicated and error-prone process. Fortunately, spmetal is automatically generated. Of course, this tool also has some restrictions. For example, the Custom field does not generate the corresponding entity, and the document library attachment does not generate. If you have such a requirement, you can only use extended object relationship ing. If you are familiar with this, do not repeat it. Instead, simply use the tools provided by Microsoft. The following describes how to use spmetal:
1. Use the defaultCodeGenerate rules
If there are no special requirements, the default rules can be metProgram.
Step 1. Open command line cmd
Step 2: CD to % ProgramFiles % \ common files \ microsoft shared \ Web Server Extensions \ 15 (replace SharePoint 2010 with 12) \ bin
Step 3. Run the command: spmetal/Web: http: // yoursiteurl/code: C: \ linqdemo. CS
You can set the namespace and serializetion of the generated code by using other parameters. For more information, see the msdn documentation.
2. Use the parameter XML file to change the default value
Using XML as a parameter to pass code generation to spmetal is an extension method that can change the name of the generated class and specify the columns contained in the list.
Generally, the object classes we want to generate are all of the same structures as those of real Sharepoint, such as the same list names and column names.
Therefore, if you do not have special requirements, do not ignore this section.
The structure of the parameter XML is:
<? XML version = "1.0" encoding = "UTF-8" ?> < Web Accessmodifier = "Internal" Xmlns = "Http://schemas.microsoft.com/SharePoint/2009/spmetal" > < Contenttype Name = "Contact" Class = "Contact" > < Column Name = "Contid" Member = "Contactid" /> < Column Name = "Contactname" Member = "Contactname1" /> < Column Name = "Category" Member = "Cat" Type = "String" /> < Excludecolumn Name = "Hometelephone" /> </ Contenttype > < Excludecontenttype Name = "Order" /> < List Name = "Team members" Type = "Teammember" > < Contenttype Name = "Item" Class = "Teammember" /> </ List > </ Web >
LINQ to SharePoint Application
This section describes some of the main content of LINQ to SharePoint. The following describes how to use LINQ to SharePoint.
I. Use the spmetal tool to generate object class declarations
// ------------------------------------------------------------------------------ // <Auto-generated> // This code is generated by the tool. // Runtime version: 4.0.30319.17929 // // Changes to this file may lead to incorrect behavior, and if // Re-generate the code, and these changes will be lost. // </Auto-generated> // ------------------------------------------------------------------------------ Using System; Public Partial Class Spefdatacontext: Microsoft. Sharepoint. LINQ. datacontext { # Region Extensibility method Definitions Partial Void Oncreated (); # Endregion Public Spefdatacontext ( String Requesturl ): Base (Requesturl ){ This . Oncreated ();} [microsoft. Sharepoint. LINQ. listattribute (name = " Policyguide " )] Public Microsoft. Sharepoint. LINQ. entitylist <policyguide> Yyguide { Get { Return This . Getlist <policyguide documentation> ( " Policyguide " );}}
Use a tool to generate an object class for a specified website and generate the above Code. The default type is CS file name + datacontext. The property yyguide indicates the document library yyguide. The name attribute is the real document library name.
Ii. query the document library using LINQ
Using(Spefdatacontext sdatacontext =NewSpefdatacontext (siteurl)){VaRItemcol = (FromPolicyInSdatacontext. policyguideWhereCondition (for example, policy. title! = NULL)
SelectPolicy). Skip (Num1). Take (Num2). Orderby (P =>P. createtime );}
The above code is just a simple example. First, create a datacontext class, so that you can use a strong-type LINQ to SharePoint. you should understand the syntax of LINQ, skip is used to obtain data starting from a specified position. Take specifies the number of data obtained, and orderby specifies the sorting rule.
LINQ to SharePoint allows you to establish connections between lists, but must have reference fields.
It should be noted here that, after all splistitems are taken out by LINQ to SharePoint and then filtered, this will cause certain performance problems. Therefore, you must use the take () method. In this way, only one data record is obtained in the generated caml statement. To view the caml statements generated by LINQ to SharePoint each time, you can obtain them through the log attribute of datacontext.
When I checked the msdn document, I found that I had explained the performance of LINQ to SharePoint. If you are interested, please refer to the msdn document.
3. Use the LINQ to SharePoint document to modify
Using (Spefdatacontext sdatacontext = New Spefdatacontext (siteurl )){ VaR Item = ( From Policy In Sdatacontext. policyguide Where ! String. Isnullorempty (Policy. ID)
Select Policy). Take ( 1 ). Single (); If (Item! = Null ) {Item. Title = "hasmodified ";
}
Sdatacontext. submitchanges ();
}
First, find the data item to be modified from the document library. After the item is modified, call the datacontext. submitchanges () method. In this way, the changed item will be updated by using LINQ to SharePoint.
Iv. Permission escalation
The current user may not have operation permissions when using LINQ to Sharepoint to operate data. For details, refer to the spsecurity. runwithelevatedprivileges method in the Sharepoint Server model.
This should be used by everyone, and it does not belong to the content of this article. It serves as only a prompt.
Summary
Summary of advantages and disadvantages:
1. LINQ to Sharepoint is strongly typed and easy to operate.
2. convenient query by using LINQ to SharePoint. You do not need to know how to write caml.
3. Not only query, but also deletion and modification are supported for LINQ to SharePoint.
4. performance problems may occur when using LINQ to SharePoint.
5. Restrictions on some lists, such as some custom content, are imposed on LINQ to SharePoint.
In any case, LINQ to Sharepoint is also a trend in operating SharePoint data, waiting for Microsoft to improve it.