Posts

Showing posts from 2015

C# Call SharePoint Web API from Server Object Model

In my last blog  SharePoint 2013 Create and Host Web Service . I have explained how to create SharePoint Web Service. So we need to consume that service. We can consume that service from JSOM as well as SSOM. Here I am explaining how we can consume web service from C# code(SSOM). So first of all we need to define same entity for getting data, Which we had used in operation contract of service. So below is an example of entity. a. Entity in Service Contract     [DataContract]     public class ModuleDetails     {         [DataMember]         public string ID { get; set; }         [DataMember]         public string ModuleName { get; set; }         [DataMember]         public string AppName { get; set; }     } b. Entity where we will consume that service     public class ModuleDetails     {         public string ID { get; set; }         public string ModuleName { get; set; }         public string AppName { get; set; }     } 1. If request type is Get                        

SharePoint 2013 Create and Host Web Service

Image
In this post we will discuss about how we can create WCF/Web service in SharePoint environment. Here you can get step by step instruction to create web service. This web service can be used to get data from share point. This service can return output in json and xml format. this service can be used in client side code as well as server side code. Suppose that there is a requirement say you want to perform same action from multiple platforms or multiple applications, So in such scenarios you can go with service. Its too simple to create and implementation is same as what we are doing in C# code. In WCF services hosting is always a challenge. But in SharePoint web service we can host on existing web application. So there is no need to put extra efforts to host SharePoint web service.  Below is the step to step instruction to create SharePoint Web Service. 1. Open visual studio with Run as administrator 2. Create new solution and select SharePoint 2013 - Empty Project  3. Pro

PowerShell Set Property Bag to SharePoint

Sometimes we want to set some configuration or wants to hard code :) some values in a solution. For the same we have some several ways and Property Bag is one of them.  Below is the PowerShell script to set property bag clear Remove-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue  Add-PSSnapin Microsoft.SharePoint.Powershell $farm=Get-SPFarm $propKey = " KeyName " $propValue = " Value " if($farm.Properties.Containskey($propKey)) {     $farm.Properties.Remove($propKey) } $farm.Properties[$propKey] = $propValue $farm.Update() We have set property bag value. But now how we can access the same from our solution. So below is the code to get property bag values by key.                SPFarm farm = SPFarm.Local;                 string propertyBagKey= " KeyName ";                 string propertyBagValue= string.Empty;                 if (farm != null )                 {                     if (farm.Properti

ASP.NET: Confirm PostBack OnClientClick Button

ASP.Net Button: <asp:Button ID="btnSubmit" runat="server" class="unrulyBtn" Text="Submit" OnClientClick="if ( ! btnSubmitonClickEvent()) return false;" OnClick="btnSubmit_Click" CausesValidation="True" /> Java Script Method: function btnSubmitonClickEvent() {         if (confirm('Once submitted, Flight Report for this sector cannot be edited.')) {          // We can't use disable here because if we make that button as disabled, In that case OnClick will not work.          // $('[id$=btnSubmit]').attr("disabled", "disabled");                          $('[id$=btnSubmit]').attr("style", "display:none");             return true;         }         else {             //   $('[id$=btnSubmit]').removeAttr('disabled');             $('[id$=btnSubmit]').attr("style", "display:block");        

SharePoint 2013: Working with User Profiles using CSOM

As we all know SharePoint 2013 provides strong support to client api. In this post we will talk about access user profile properties using client api. So now in SharePoint 2013 we can directly query to user profile and get all the user profile properties what we required. References which we need to provide <script src="/_layouts/15/jquery-1.9.0.min.js"></script> < script   src = "/_layouts/15/SP.Runtime.js" ></ script > <script src="/_layouts/15/SP.js"></script> <script src="/_layouts/15/SP.UserProfiles.js"></script> 1. Get User Profile Properties for Current User        $(document).ready(function(){        // Ensure that the SP.UserProfiles.js file is loaded before the custom code runs. SP.SOD.executeOrDelayUntilScriptLoaded(loadUserData, 'SP.UserProfiles.js'); }); var userProfileProperties; function loadUserData(){ //Get Current Context v

C# Copy files from one server to another

Sometimes we need to copy files from one sever and paste those files to another server programmatically. So for that we can use below code but we should have admin credentials of that server where we want to paste files. In below method we need to pass source path of the server, from where we want to copy and destination path of the server, where we want to paste. Also we need to provide UserName, Domain, Password of the server where we want to paste those files. public void CopyFiles(string sourcePath, string destinationPath)         {             try             {                  IntPtr admin_token = new IntPtr();                 // use LOGON32_LOGON_NEW_CREDENTIALS as LogonType                 if (LogonUser(" UserName ", " Domain ", " Password ", 9, 0, ref admin_token) != 0)                 {                     WindowsIdentity wid_admin = new WindowsIdentity(admin_token);                     WindowsImpersonationContext wic =