Click here to Skip to main content
15,113,214 members
Articles / Productivity Apps and Services / Sharepoint / SharePoint 2013
Tip/Trick
Posted 22 Nov 2013

Tagged as

Stats

16.9K views
2 bookmarked

SPMetal in Sharepoint 2013

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
22 Nov 2013CPOL1 min read
SPMetal in Sharepoint 2013

What is SPMetal?

SPMetal is a command-line tool that generates entity classes, which provide an object-oriented interface to the Microsoft SharePoint Foundation content databases. These classes are primarily used in LINQ to SharePoint queries; but they are also used to add, delete, and change list items with concurrency conflict resolution. Finally, they can be used as an alternative to the regular SharePoint Foundation object model for referencing content.

How to Use SPMetal?

To generate the entity class using SPMetal:

Step 1

Go to the path

C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\BIN

Open command Window here:

Image 1 

Step 2

Run the command here:

Image 2

C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\BIN>Spmetal.exe /web: http://Your Site/ /code: [File location to save]/Myentitycontext.cs

After that entity class file generated.

Image 3

Different Command Line Options

Here, http://msdn.microsoft.com/en-us/library/ee538255.aspx mentions the different command line options for Spmetal.

Image 4

Using in Visual Studio

When you are creating an application using this entity class, you need to add the reference Microsoft.Sharepoint.LINQ.

In code behind of Webpart, add the following namespaces:

C#
using System.Linq;
using Microsoft.SharePoint.Linq;
using Microsoft.SharePoint;

Insert a value to “Country” List

Create a list in SharePoint and insert a value through VS2012 console application:

Image 5 

C#
using (MyentitycontextDataContext myEntity = new MyentitycontextDataContext("http://sptest:1001"))
            {
                CountryItem myCountry = new CountryItem();
                myCountry.Title = "My Country1";
                myCountry.CountryName = "USA";
                myEntity.Country.InsertOnSubmit(myCountry);
                myEntity.SubmitChanges();               
            }

Now refresh the site the value is inserted:

Image 6

References

Summary

In this tip, I tried to explain how to create entity class and using for Read, Insert, Update, and Delete using LINQ to SharePoint.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

shijuse
Software Developer (Senior) EY
India India
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --