Search This Blog

Friday, November 10, 2017

Get Logged User / Current user Details Using Rest Api


//Call  the function  Where You Want to use 

getUser(_spPageContextInfo.userId);
 
 // Fuction Body
function getUser(id){
var returnValue;
  jQuery.ajax({
   url: vUrl +"/_api/Web/GetUserById(" + id + ")",
   type: "GET",
   async:false,
   headers: { "Accept": "application/json;odata=verbose" },
   success: function(data) {
           var dataResults = data.d;
      //get login name 
      var loginName  = dataResults.LoginName.split('|')[1];
      var Email=dataResults.Email;
      returnValue=loginName+";"+Email;
     // returnValue=loginName;
   }
  
 });
return returnValue;
 
}

Consume WCF and REST WCF Services in Console and C#

class Program
    {
        static void Main(string[] args)
        {
            
            string ngoServiceUrl ="https://ngo.com/NGO/ngo_services.svc/GetUserinfoData"
            int oprtype = 3;
            string txtEmp="ngo\\\\shrawan";
            WebRequest req = WebRequest.Create(ngoServiceUrl );
            req.ContentType = "application/json; charset=utf-8";
            req.Method = "POST";
            string da = "{\"userid\":\"" + txtEmp + "\",\"oprtype\":" + oprtype + "}";
            using (var streamWriter = new StreamWriter(req.GetRequestStream()))
            {
                streamWriter.Write(da);
                streamWriter.Flush();
            }
            var httpResponse = (HttpWebResponse)req.GetResponse();
            if (httpResponse.StatusCode == HttpStatusCode.OK)
            {
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var responseText = streamReader.ReadToEnd();
                    string[] res=responseText.Split(':');
                    string msg = res[2].Split('}')[0];
                    if (msg.Equals("1"))
                    {
                        Console.WriteLine("Redirect");
                    }


                }
            }


                                        
        }


        }

Using SharePoint Lists vs. Database Tables

An important decision in the design of the Training Management application was deciding whether to store information in lists or in a database. SharePoint lists consist of rows and columns that store data in a similar fashion to a traditional relational database management system such as SQL Server. However, a benefit of lists is that SharePoint includes Web Parts that provide simple methods for managing the data. If the data was stored in a database, it would require custom user interface components to access it and manipulate it. Also, specialized skills are required to design, implement, and maintain a custom database. Another advantage of using lists is that custom workflow and event handlers can easily be registered to them.
There are also advantages to storing data inside of a database. One is the availability of all the ACID (Atomic, Consistent, Isolated and Durable) properties of transactions. If your business logic requires transactions, storing data in a database is preferable to using lists. Also, SharePoint lists are meant to store simple data structures. If you require a complex data model with intricate relationships, a database is more appropriate. The Training Management application has three data storage requirements. It stores data related to training courses, registrations, and registration approval tasks. All the data is relatively simple and does not use transactions. The registration data also requires that there be a workflow. All these reasons make SharePoint lists the appropriate choice.
In general, reading and writing to a custom database provides an overall performance advantage. Although SharePoint has made significant strides in its performance, there is still a certain amount of overhead involved in processing lists. The SharePoint product team recommends limiting the number of items in a list to 2,000 for each list container. (A container is the root of the list and any folders in the list.) You may be able to manage larger lists by using views that are filtered so that no single view contains more than 2,000 items. Another way to circumvent this performance degradation is to write custom interfaces to manage and retrieve the data. (It is important to note that the 2,000 item threshold has more to do with HTML rendering performance than with the underlying performance of lists as a storage mechanism.) For more information about the performance of SharePoint lists, see the SharePoint product group's white paper, Scaling to Extremely Large Lists and Performance Access Methods.
The following table summarizes the benefits of using databases and SharePoint lists.
Benefits
Database
SharePoint list
Handles complex data relationships
Yes
No
Handles large numbers of items
Yes
No
Handles transactions
Yes
No
Is easy to use
No
Yes
Accommodates workflows
No
Yes
Includes a standard interface
No
Yes
Can easily add binary data
No
Yes


Best Practices for jQuery Developers

Following are some of the best practices that you could follow while working with jQuery:
  • Plugins:
    • Please avoid unnecessary use of plugins
    • If a plugin is being used, respect the authors work and ensure that the plugin file is reused with the license comments intact
    • If you are writing a reusable functionality in the code, always move it into a jQuery plugin format, to be reused across the site
  • Coding:
    • Use jQuery CDN (google or Microsoft or jQuery) as much as possible for script registration, if its fine with clients. To be on the safer side, you could have a fallback code as well:
      <!– Grab Google CDN jQuery. fall back to local if necessary –> 
<script>!window.jQuery && document.write(‘<script src=”js/jquery-1.4.2.min.js”><\/script>’)</script>
  • Avoid unnecessary upgrades of jQuery, unless there are features, which you require or some plugins require. Stick to one version from the beginning. (ex. 1.5.1)
  • Avoid writing event handler attributes (onclick etc.), instead use live or bind methods to attach the respective event handlers.
  • Avoid mixing javascript code with jQuery code.
  • Cache any jQuery selector, if reused in multiple statements.
    Ex. Avoid $(“#x”).val(); $(“#x”).attr(“align”,”right”); Use var $x = $(“#x”); $x.val();$x.attr(“align”,”right”);
  • Ensure that you place ready function in your code. $(document).ready(function(){});
  • Use find method, instead of building a complex jQuery selector.
//Fine in modern browsers, though Sizzle does begin “running”
$(‘#someDiv p.someClass’).hide(); 
// Better for all browsers, and Sizzle never inits. 
$(‘#someDiv’).find(‘p.someClass’).hide(); 
  • Avoid misusing $(this).
    Ex. Use this.id instead of $(this).attr(‘id’)
  • Keep your code safe, by using noConflict() method or by passing jQuery.
    Ex. (function($){})(jQuery); or by wrapping it in a ready method.
  • Avoid declaring new jQuery selectors within foreach, instead declare them outside and reuse it inside the loop.
  • Use $.ajax instead of $.get or $.getJSON, because internally they call $.ajax.
  • Use JSON formats for communications.
  • Ensure that you move your jQuery codes to a separate javascript file, instead of inline scripts.
  • Compress javascript files for better performance.
  • Combine multiple css() calls or attr() calls into one. In case of attributes, you could also pass them as shown below: 
$(‘</a>’, {
    id : ‘myId’, 
    className : ‘myClass’, 
    href : ‘mytest.html’ 
});
  • Debugging:
    • Use Developer Toolbars and Inspectors to debug your code from client side. You do not need to perform deployments and builds to debug your jQuery code(s).
  • jQuery Cheat Sheet:
    • Always keep a jQuery cheat sheet handy with you to know about the list of functions available for you. Download here
Below are some more useful links you could check out:
Some words of caution:
  • $.ajax has issues in making cross domain AJAX calls. As a solution, you can get a patched version of $.ajax from the net.
  • Avoid http request on https sites. Your user will be prompted with a nagging security popup, which on rejection can break your request.
  • Avoid loading multiple jQuery versions.
  • Majority of the jQuery plugins are browser compatible, but NOT ALL. Read the plugin documentations carefully and use them judiciously.
This might appear to be a drop from the ocean. But it will guide them in the right direction.