IT tutorials
 
Technology
 

LINQ to SharePoint and SPMetal : Adding Data Using LINQ (part 1) - Add Data Generation Buttons to LinqSampleApplication , Add Sample Contracts

11/26/2013 8:25:38 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
Our data structure consists of two lists, On-Hire Assets and Asset Notes, and a document library, Hire Contracts. Document libraries contain document-based content, and as a result, content types that are used in a document library must be derived from the built-in Document content type. When we created our Hire Contract content type, we based it on the Document content type to meet this requirement. By using a Document content type, we’re dictating that all items must have an associated document, and this presents a problem for LINQ.

LINQ to SharePoint is a data manipulation tool designed to deal with the actual data that’s associated with an individual item. The entity classes are created based on the fields defined in the associated content types as opposed to the properties of the object that manages the content. For example, most data items in SharePoint are represented by the object model as an SPListItem object. The SPListItem has many properties, such as Title, Url, and UniqueId. None of these properties is mapped to an entity object for use by LINQ to SharePoint; instead, LINQ to SharePoint focuses only on the values of the fields that are associated with the SPListItem. As a result, it’s impossible to manipulate any properties of an SPListItem object (or any other object) using LINQ to SharePoint. In the case of a Document-based item, we can’t create an object using LINQ, because items stored in a document library must have an associated file. Since File is a property of the SPListItem object, we cannot set it via LINQ.

Since we can’t add documents to our document library using LINQ, we can write some SharePoint code that makes use of the object model to insert some sample data quickly.

Add Data Generation Buttons to LinqSampleApplication

In the LinqSampleApplication project, open Form1.cs. From the toolbox, drag three button controls, a textbox, and a label onto the design surface. The form designer should look like this:

Add Sample Contracts

Double-click the Add Sample Contracts button to add some code for the on-click event, and then add the following code:

private void button1_Click(object sender, EventArgs e)
{
//Add some sample data to the hire contracts document library
//disable the button to prevent concurrency problems
button1.Enabled = false;
using (SPSite mySite = new SPSite(SiteUrl.Text))
{
using (SPWeb myWeb = mySite.OpenWeb())
{
for (int x = 0; x < 3; x++)
{
//Since we're not interested in the document contents,
//create a simple text document
string test = x.ToString("This is test contract" +
" number 000");
byte[] content = System.Text.Encoding.UTF8.GetBytes(test);

//Get a reference to the Hire Contracts list

SPList HireContracts=myWeb.Lists["Hire Contracts"];

//Generate a sequential file name

string filename=x.ToString("SampleContract000.txt");

//Upload the file to the document library
SPFile newFile = HireContracts.RootFolder.Files.Add(filename,
content);

//Get a reference to the SPListItem that is automatically
//created when the document is uploaded

SPListItem item =newFile.Item;

//populate the mandatory fields with sample data

item["ContractId"] = x.ToString("CONT-000");
item["Contract Start Date"] = DateTime.Now.AddMonths(0-x);
item["Contract End Date"] = DateTime.Now.AddMonths(x+12);

//Persist the changes to the content database
item.SystemUpdate();
}
}
}
//change the text on the button so that we know the job's done
button1.Text += " - Done";
}



As you’ll see from the comments, this code simply creates a few dummy Hire Contract documents in our document library. Once the documents have been uploaded, the properties are set using indexed properties on the SPListItem object. You’ll notice that the field names and the name of the document are not strongly typed. This is the problem we are addressing by using LINQ.

 
Others
 
- Windows 7 : Kernel Mode Installation and Build - Using WinDbg with Featured Toaster, Versioning and Dynamic Binding
- Windows 7 : Kernel Mode Installation and Build - Debugging Macros and Routines, WDF Debugger Extension Commands
- Microsoft Excel 2010 : Introducing the Excel Web App - Sharing a Workbook, Interacting with a Sheet Online
- Microsoft Excel 2010 : Uploading and Downloading Workbooks (part 2) - Saving to SkyDrive from Excel, Saving a File to Your Local Drive
- Microsoft Excel 2010 : Uploading and Downloading Workbooks (part 1) - Accessing the SkyDrive, Uploading with Windows Live
- Microsoft Excel 2010 : Introducing the Excel Web App - Acquiring a Windows Live ID
- Deploying Windows in a Windows Server 2008 R2 Environment (part 6) - Practice Create a Windows PE CD
- Deploying Windows in a Windows Server 2008 R2 Environment (part 5) - Windows Deployment Methods - Windows Deployment Services
- Deploying Windows in a Windows Server 2008 R2 Environment (part 4) - Windows Deployment Methods - Installing from the Product DVD, Network Share Distribution
- Deploying Windows in a Windows Server 2008 R2 Environment (part 3) - Windows Deployment Fundamentals - Deployment Image Servicing and Management
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us