Skip to main content

Posts

Showing posts with the label CSOM

How to Send Email in SharePoint Provider Hosted Apps

There are few methods that we can use to send Emails in SharePoint Provider Hosted apps. Using general Email Sending method Using SharePoint Client Object Model (CSOM) Using SharePoint JavaScript Model (JSOM) Using general Email Sending method This is the general method we are using for sending email for asp.net. This is method has advantages over other two methods. this method has few advantages over other methods, Send attachments to the recipients Send emails to external users (SharePoint 2013 email function can not be used to send emails to external users)  There are many articles in available for this , I’m describing a sample code below. MailMessage mail = new MailMessage( "from@mail.com" , "to@mail.com" ); SmtpClient client = new SmtpClient(); client.Port = 25; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false ; client.Host = "smtp.google.com" ; mail.Subject = "this is a test email." ; m...

Working with SharePoint folder properties using CSOM (Client Side Object model ) in SharePoint 2013

  Accessing folder properties are but different from  SharePoint list item access. Following code demonstrates  the basics. Create SharePoint Folder with Properties using (var clientContext = ypurMethodToGetSpContectt() ) {   var lst = clientContext.Web.Lists.GetByTitle( "ListName" ); var folder = lst.RootFolder.Folders.Add( "FolderName" ); folder.Properties[ "property1" ] = "sample" ; folder.Properties[ "property1" ] = "sample" ;   folder.Update(); clientContext.ExecuteQuery();   }

How to Add Multiple Users in Data Element in SharePoint List Scheme

This is method is also use for add multiple lookup values as well. < ListInstance Title ="List1" OnQuickLaunch ="FALSE" TemplateType ="100" Url ="Lists/List1" Description ="List1 List Instance" > < Data > < Rows > < Row > < Field Name ="Title" > abc </ Field > < Field Name ="MultipleUser" > 9;#Melick Baranasooriya;#12;#One User </ Field > < Field Name ="MultipleValues" > 1;#One;#2;#Two </ Field > </ Row > </ Rows > </ Data > </ ListInstance > In here separator will be ;# thus we need to specify id ;# value

Working with client side relative Url Tokens in SharePoint 2010/2013

Most of the time we are having problems with Getting relative site collection URL in client side Getting relative Layout URL in client side Getting web site URL in client side when we are developing SharePoint applications. (for an example Creating a custom master page and providing links to content inside the SharePoint). If we are working with SharePoint  Standard or Enterprise we can  use < link rel= "stylesheet" type= "text/css" runat= "server" href= "<% $SPUrl:~sitecollection/Style%20Library/mystyle.css %>" /> But to work with SPUrl you need the control to be server control ( runat = server ). Other Possible Url tokens are ~site/ ~sitecollection/    ~language there are few other undocumented Tokens as well . Other than that you can use _spPageContextInfo JavaScript Objet in all SharePoint versions. <script type= "text/javascript" > function goToLink(link) { location.href = _spPageContextInfo.web...

How to Get SharePoint Client Context in SharePoint Apps (Provider Hosted / SharePoint Access ) in CSOM (Client Side Object Model)

First step of the of the app development is to correctly get the access to SharePoint client context. I have struggling with develop a simple model to initialize the SharePoint Client context. Most of the App development include ASP Master pages. So I need to figure out a working model for app development. First you need to know how SharePoint offer the contextString . contextString Offers when SharePoint is getting redirect from appredirect.aspx url. Ex :- https://rajee.sharepoint.com/_layouts/15/appredirect.aspx?instance_id={B0D5D768-303F-4AE7-A4D3-F94B687C6AB3 that point we need to capture the contextString and generate either AccessToken or RefreshToken and save it for access the SharePoint  Client Context in later time. other wise it will result in generating the  error The parameter 'token' cannot be a null or empty string (This is nasty error which drove me crazy) Normally AccessToken is valid for 12 hours and RefreshToken is valid for 6 months. In my approach ...

Break Permission inheritance and add custom permission to SharePoint 2013 List using JSOM (JavaScript Client Object Model)

First you need to refer relevant JavaScript files. <script type= "text/javascript" src= "../Scripts/jquery-1.7.1.min.js" ></script> <script type= "text/ecmascript" src= "/_layouts/SP.core.debug.js" /> <script type= "text/ecmascript" src= "/_layouts/SP.runtime.debug.js" /> <script type= "text/ecmascript" src= "/_layouts/SP.debug.js" /> Then approach is Get reference to current clientcontext get web site reference get reference to existing list break the inheritance define a role get the user finally add user and related role to the list <script type= "text/ecmascript" > function CustomPermision() { var clientContext = new SP.ClientContext.get_current(); var oWebsite = clientContext.get_web(); // provide the list name oList = clientContext.get_web().get_lists().getByTitle( 'ListName' ); oList.breakRol...

Add Fields to SharePoint 2013 List Using JSOM (ECMAScript Client Object Model)

Other Related Posts Creating a List In My previous post i described how to add a list to SharePoint. In here I'm going to  describe how to add fields to existing list.  First you need to refer same JavaScript files in your script page ( SP.core.js,SP.runtime.js,SP.js )  There are few approaches that you can use to add fields to existing list. But in my personal opinion i like following approach because It is easy rather that use XML to add entire field Intelligence are support based on field type (text, choice, user ..) Code is more readable than xml <script type= "text/ecmascript" > function addFields() { var clientContext = new SP.ClientContext.get_current(); var oWebsite = clientContext.get_web(); //Geting reference to the list oList = clientContext.get_web().get_lists().getByTitle( 'CustomList' ); // Get filed collection var fldCollection = oList.get_fields(); va...

Create Client Context To Access SharePoint 2013 (Office 365 Preview) in AutoHosted SharePoint App Model using SharePoint client object model (CSOM)

If we are using SharePoint app model AutoHosted Environment we need to use Client Context model to access SharePoint. Therefore First you need to get a Valid Access Token from SharePoint Server. For that you need to pass the SharePoint Site Url (This is available as SPHostUrl in the Query String) and Context Token that we can generate from passing the request Object . TokenHelper Class provides methods that used can be used to access the SharePoint server and generate Access Tokens. 1. Get the Context Token by passing the HttpRequest String context = TokenHelper .GetContextTokenFromRequest(Request); 2.Then get the Uri from the query string which provides the path to SharePoint server. Uri SharePointUri = new Uri (Request.QueryString[ "SPHostUrl" ]); 3. Then validate and generate access Token SharePointContextToken contextToken = TokenHelper .ReadAndValidateContextToken(context, SharePointUri.Authority); String AcessToken = TokenHelper .GetAccessToken(contextToken, shar...