erotik film
bodyheat full moves www xxx kajal video la figa che sborra ver video de sexo porno
Luxury replica watches
escort antalya

** THIS SITE IS AN ARCHIVE ** - New Content is at:

Jun 8

Written by: Michael Washington
6/8/2014 9:10 PM  RssIcon


You can create a Visual Studio LightSwitch SharePoint enabled Cloud Business App (CBA) that consumes and updates SharePoint Lists, including uploading documents to SharePoint Document Libraries. You can even dynamically create SharePoint Document Libraries in code so that they are available for use in your multi-tenant provider-hosted SharePoint applications.

Note: To complete this tutorial you must have Visual Studio 2013 with the Visual Studio 2013 Update 2 (or higher) installed.

To understand how documents are integrated into a CBA app, we will first walk-thru the completed application:


We install the CloudBusinessTasks application and click on it to run it.


The application loads.


A message indicates that a SharePoint List called TaskDocuments has been created.


We create a Task.


We click on the Task we just created to edit it.


We select the Documents tab.


We select Add Document.


We have the option to upload a document or create one.

We decide to create a new Microsoft Word Document.


The document is created, we edit it, and we navigate back to the website.


In the SharePoint site, we see the document has automatically been saved and uploaded to the TaskDocuments library.

We can click on the CloudBusinessTasks link to navigate back into the application.

(note: while we were editing the document, we could have clicked the web browser ‘back’ button to automatically save the document and navigate directly back to the application)


We note that this time the message indicates that the Document Library already exists.

We can click on the Task to see the details.


Clicking on the Documents tab shows us all the documents associated with the Task.


Clicking on a document opens it up.

Creating The Application


We start with the application created in: Creating A LightSwitch SharePoint 2013 Multi-Tenant Provider-Hosted Application.

Create The Document Library List


First, we need to create a SharePoint Document Library List.

Log into your SharePoint Development site, and click add an app under Site Contents.



Add a Document Library.


Call it TaskDocuments.


After the list is added to the list of apps in Site Contents, select the SETTINGS.

We now need to add a column (TaskID) to allow us to associate a Document to a Task.


In the Settings, select Create column.


Call the column TaskID and set its type to Single line of text.

This is so that each document can be associated with a Task in the application.

Click OK.

Connect To The Document Library


In Visual Studio, right-click on Data Sources and select Add Data Source.


Select SharePoint.


Enter the address of your Development SharePoint server.


You may have to log in.


Select the TaskDocuments from Document Libraries, and UserInformationList from Lists.


The lists will show in the project.

Enhance The Task Table


Open the Task table and create a field called TaskID.

Select Write Code then Tasks_Inserting.

Use the following code for the method:


        partial void Tasks_Inserting(Task entity)
            // Set the HostURL
            entity.HostURL = this.Application.SharePoint.HostUrl.Host;
            // Create a unique ID for this Task
            // so that we can also store it in the Document
            // Library document to relate it to the Task
            entity.TaskID = System.Guid.NewGuid().ToString();




  1. Return to the Task table and click the Relationship button.
  2. Select TaskDocument as the To table.
  3. Set the Multiplicity to One to Many.
  4. Set TaskID as the Primary and Foreign keys.


The relationship will show.

Update The Screens – Documents


Open the ViewTask screen and select the Add TaskDocuments link (that now shows because we created a relationship between the Task table and the TaskDocuments SharePoint Document Library list).


Select the Tabs node and then select Add Tab.


Select the Tab that was just added and change it’s name in its Properties to Documents.

select Add under the section and then select the Task Documents collection.


Select the Command Bar to open it, and click Add to add a Button.

For the Button method, select the TaskDocuments.createOrUpdateDocument method.


In the Properties for the Button change its name to AddDocument.

Update The Screens – Main Screen


We will now create JavaScript on the first page, that will call a handler (StartUpTasks.ashx), that will dynamically create the SharePoint Document Library List (if needed).

Open the BrowseTasks screen and add a New Custom Control to the screen.


Use the default data path.


Drag the control to the top position.


In the Properties for the control, set the Label Position to None.


Also, in the Properties for the control, select Edit Render Code.

Use the following code for the method:


myapp.BrowseTasks.ScreenContent_render = function (element, contentItem) {
        type: 'post',
        data: {},
        url: '../StartUpTasks.ashx',
        success: function success(result) {
            // Parse the JSON returned
            //attach the permissions to the global 'myapp' object 
            //so it is accessible to the client anywhere.
            myapp.settings = jQuery.parseJSON(result);
            var strHeader = "";
            // Loop through each row returned
            $.each(myapp.settings, function () {
                var strCurrentHeader = this.Type;
                var strValue = this.Value;
                // Only show header if it has changed
                if (strHeader !== strCurrentHeader) {
                    // Update strCurrentHeader
                    strHeader = strCurrentHeader;
                    $(element).append("<b><u>" + strHeader + "</u></b><br/>");
                // Show value of the current row
                $(element).append(strValue + "<br/>");


The Handler - Create The SharePoint List (If Needed)


We will now create the handler, that will dynamically create the SharePoint Document Library List (if needed).

Right-click on the Server project and select Add then New Item.


Create a Generic Handler called StartUpTasks.ashx, and use the following code for the handler:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.LightSwitch.Server;
using Microsoft.LightSwitch.Security;
using Microsoft.LightSwitch.Security.Server;
using LightSwitchApplication;
namespace LightSwitchApplication
    public class setting
        public string Type { get; set; }
        public string Value { get; set; }
    public class StartUpTasks : IHttpHandler
        public void ProcessRequest(HttpContext context)
            List<setting> settings = new List<setting>();
            using (ServerApplicationContext ctx = ServerApplicationContext.CreateContext())
                var currentUser = ctx.Application.User;
                if (currentUser.IsAuthenticated)
                    // Call the SharePoint CSOM API
                    var clientContext = ctx.Application.SharePoint.GetHostWebClientContext();
                    // Get the current web 
                    Microsoft.SharePoint.Client.Web web = clientContext.Web;
                    // Check to see if SharePoint List 'TaskDocuments' exists
                    Microsoft.SharePoint.Client.ListCollection lists = web.Lists;
                    var TaskDocumentsList = (from objTaskDocument in lists.ToList() 
                                             where objTaskDocument.EntityTypeName == "TaskDocuments" 
                                             select objTaskDocument).SingleOrDefault();
                    if (TaskDocumentsList != null)
                        // List exists - show it
                        settings.Add(new setting()
                            Type = "TaskDocuments SharePoint List [Exists]",
                            Value = string.Format("Title: {0} - EntityTypeName: {1} - ItemCount: {2}",
                        // List does not exist - Create it
                        Microsoft.SharePoint.Client.ListCreationInformation creationInfo =
                            new Microsoft.SharePoint.Client.ListCreationInformation();
                        creationInfo.Title = "TaskDocuments";
                        creationInfo.TemplateType = (int)Microsoft.SharePoint.Client.ListTemplateType.DocumentLibrary;
                        Microsoft.SharePoint.Client.List lstTasksDocuments = web.Lists.Add(creationInfo);
                        lstTasksDocuments.Description = "Task Documents";
                        // Add TaskID field
                        Microsoft.SharePoint.Client.Field fldTaskID =
                            "<Field DisplayName='TaskID' Type='Text' />",
                        Microsoft.SharePoint.Client.FieldNumber fldNumber =
                        // Show the list 
                        Microsoft.SharePoint.Client.List TaskDocumentsListCreated = web.Lists.GetByTitle("TaskDocuments");
                        settings.Add(new setting()
                            Type = "TaskDocuments SharePoint List [Created]",
                            Value = string.Format("Title: {0} - EntityTypeName: {1} - ItemCount: {2}",
                    settings.Add(new setting() { Type = "", Value = "You are not authenticated" });
            // Create JavaScriptSerializer
            System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer =
                new System.Web.Script.Serialization.JavaScriptSerializer();
            // Output as JSON
        public bool IsReusable
                return false;



So that the System.Web.Script.Serialization.JavaScriptSerializer code will work, right-click on References for the project, and add a reference to System.Web.Extensions.

Build And Deploy

Follow the directions in the article: Creating A LightSwitch SharePoint 2013 Multi-Tenant Provider-Hosted Application to build and deploy the project.

Download Code

The LightSwitch project is available at

(you must have Visual Studio 2013 (or higher) installed and a SharePoint Developer site to run the code)

Special Thanks

A special thanks to Josh Booker, Matt Evans, Eric Erhardt, and Dave Kidder and for assistance in creating this article.

Links – Microsoft

Integrating Documents in Cloud Business Apps

Using SharePoint Lists as LightSwitch Entities and with CSOM (Chris Rummel)

Incident manager: A cloud business app tutorial

How to: Associate a document library with an entity

How Do I: Incorporate Document Libraries in a Cloud Business App?

Troubleshooting document libraries

Links – LightSwitch Help Website

Deploy A LightSwitch Application To Office 365 / SharePoint Online

Creating A SharePoint Online Testing Site

Exploring SharePoint 2013 Visual Studio Cloud Business Apps (LightSwitch)

Creating A LightSwitch SharePoint 2013 Multi-Tenant Provider-Hosted Application

Embedding Your LightSwitch Cloud Business App Inside SharePoint As a App Part

12 comment(s) so far...

Michael, is your event handler for creating the SharePoint list necessary anymore? Or can the list be created and deployed without code in current versions of Visual Studio? I would like to understand the pros and cons of each method.

By ThoroughbredTech on   12/10/2014 10:25 AM

@ThoroughbredTech - I was unable to get the no code list creation to work. Also, you don't need that code if you create the list manually. However, if you have an app in the app store you don't want to require your customers to have to carefully create any needed lists :)

By Michael Washington on   12/10/2014 10:32 AM

Hi, Anyway to link one task to a folder in the document library and thus create documents in that folder? As I have a requirement where documents with the same name can be created for different tasks and associating a task to a folder will prevent the duplicate name error that I encounter now. Thanks.

By Sivakumar Angarai on   8/21/2015 1:37 PM

@Sivakumar Angarai - I don't know, but, you can use the CSOM API code to do practically anything. You can search the SharePoint sites for CSOM examples. All CSOM API code will work with LightSwitch.

By Michael Washington on   8/21/2015 1:39 PM

Hi Michael. I'm a big fan, bought your book :) Hoping you can help me. I downloaded and deployed this app. If I run it by debugging in visual studio then all works fine. If I deploy it to my IIS server though, I run into two symptoms of what might be the same problem. First, the TaskUser property of type Person doesn't work. It doesn't find any users. Second, when I click 'Add' to add a document, I get "Error accessing the SharePoint site". I googled some answers about missing relations between the tables, but that's not my problem. the relations are good, I didn't change your app, the document library gets created in sharepoint. My gut feel is this is a permission issue somehow, that running on the IIS server doesn't work but debugging and it running on IIS Express does work. I tried several accounts for the application pool for IIS, but no luck. Any suggestions? Thanks

By dave jorg on   11/26/2015 3:59 AM

@dave jorg - It is now not working for me either :( I will see if I can get this working over the next week. If I can't I will pull the chapter out of the book :( Sorry about that. This worked for over a year.... (sigh) I created a Connect Issue:

By Michael Washington on   11/26/2015 10:50 AM

Thanks Michael. I'm wondering if it is a cross domain library issue. I THINK that would fit the symptoms as to why it works when debugging / using IIS Express (I suspect app doesn't end up in a different domain) but deploying to my IIS Web App (which is in a different domain) exhibits the problems. A little more information - I also am reading the file contents within lightswitch. Using the app (when deployed to IIS) and just trying to open the file with a clientContext fails with 401 unauthorized (note that it works when debugging), but if I replace the clientContext.Credentials with a new NetworkCredential(myName, myPassword) then I can open the file when deployed to IIS. Hope that helps.

By dave jorg on   11/26/2015 11:35 AM

@dave jorg - It is probably a cross domain library issue but this has worked this way for a year. If the process has changed we need new guidance from Microsoft. I hope they respond to the connect ticket:

By Michael Washington on   11/26/2015 11:37 AM

Hello again Michael. Little more information - I think I'm on the right track about the cross domain library access. I added the doamins to the same security zone as per and now when clicking the add button I get 'Unable to get property 'sharePointImagesUrl' of undefined or null reference' instead of the previous unable to access the sharepoint site error. I'm out of time for today, but will fiddle around more later. Thanks

By dave jorg on   11/26/2015 1:12 PM

Yet more information - adding the domains to the same security zone now allows the app to read from the library without needing to explicitly set the credentials, so just down to getting the add button to work.

By dave jorg on   11/26/2015 1:12 PM

Hi Michael,

i was also expecting that previous file should be overwrite with new one but doesn't happen.

no overwrite option here .

By mshahnawaz on   10/25/2016 4:18 AM

@mshahnawaz - Perhaps there is a setting on your SharePoint site or collection. The only other thing I could suggest is adding code to delete the current file first (sorry I have no examples).

By Michael Washington on   10/25/2016 4:22 AM
Microsoft Visual Studio is a registered trademark of Microsoft Corporation / LightSwitch is a registered trademark of Microsoft Corporation