Tuesday, May 21, 2013

Just for myself - SharePoint

Site: Permanent
Workspace: Temporary / Disposable

Document Workspace
  • Similar to Team Site
  • Complex Documents e.g. Annual Report
  • Disposable Resources - used temporarily for e.g. a week or month
Meeting Workspace
  • Agenda
  • List of Attendees
  • Objectives
  • Document Library

Monday, May 20, 2013

Just for myself - JavaScript and Ajax

  1. XMLHttpRequest Object  (XHR)
XMLHttpRequest object is used to exchange data with a server behind the scenes.

With XHR we can
  1. Update a web page without reloading the page.
  2. Request a data from a server after the page has loaded.
  3. Receive data from a server after the page has loaded.
  4. Send data to a server in background. 
Remember: Update, Request, Receive, Send

To find more about XMLHttpRequest, visit  XMLHttpRequest


  • A synchronous XHR example  : The code snippet below illustrates synchronous XHR call. It reads text from a text file named 'data.txt' and presents in the browser. 
    var request = new XMLHttpRequest();
    request.open('GET', 'data.txt', false);
    request.send();
    console.log(request);
    document.writeln(request.responseText);
  • Asynchronous XHR example: The code snippet below illustrates asynchronous XHR call. It reaqds text from a text file named 'data.txt' and presents in the browser. It also takes in consideration the browser type, e.g. it uses XHR request for browsers like Safari, Chrome, Firefox etc and uses ActiveX for Internet explorer.
var request;
/*
If the browser API has XHR object, then set request variable to new XHR object.. for Safari, FireFox, Chrmoe
*/
if (window.XMLHttpRequest) {
    request = new XMLHttpRequest();
}
else request = new ActiveXObject("Microsoft.XMLHTTP");     // else... for IE
// This makes the script compatible to all browsers
request.open('GET', 'data.txt');  // by default, third parameter is true
request.onreadystatechange = function () {
    if( (request.readyState === 4) && (request.status === 200))
    {
        console.log(request);
        document.writeln(request.responseText);
    }
}
request.send();
Both of the request example above simply writes to a console of a browser.
But we can do the same to modify any part of a web element.


  • getElementById:
    Lets say we have a div element as shown below
    <div id = "heading">
    we can use following DOM command to replace its content as document.getElementById(heading).innerHTML = request.responseText
  • getElementsByTagName:
    Lets say we have some collection of elements, e.g. unordered list as shown below

    <ul>
         <li></li>
         <li></li>
         <li></li>
         <li></li>
         <li></li>
    </ul>


    And we only want to update the third list item, then we can achieve it as shown under.

    var result = document.getElementsByTagName("li");
    result[2].innerHTML = request.responseText



Change URL of the emails send in a Document Library Alert

Hi friends,

If we are subscribed to alert on any SharePoint list or library, we get email notifications along with link to the list or library.
Lets assume the following scenario
Internal Web Application URLhttp://sp-realm/
Public web application URLhttps://access.sharepointrealm.com
Document LibraryResults
Internal URL of Document Libraryhttp://sp-realm/Results

When we create an alert for this library, we get email with the internal link to this document library by default.
If we want to associate public URL with this library, all we have to do is change Alternate Access Mapping and provide public URL in place of Default Zone for that particular web application.

Good Luck !

Balanced Parenthesis Algorithm using Recursive Function

Hi Friends,

Few days ago, I was asked to write a Parenthesis Balancing algorithm as a small test by one of the hiring company. I did it on time and have thought to share it with you guys.

The question goes as under.

Write a function which verifies parenthesis are balanced in a string. Each open parenthesis should have a corresponding close parenthesis and they should correspond correctly.

For example, the function should return true for the following strings:

  • (if (any? y) sum (/1 y))   
  • I said (it's not (yet) complete). (she didn't listen)
The function should return false for the following strings: 
  • :-)
  • ())(
Optional Bonus: Implement the solution as a recursive function with no mutation / side effects. 

Besides the question asking for only the small parenthesis, this program can work on any kind of parenthesis like  
i. < , >
ii. { , }
iii. [ , ]

All you have to do is provide the type of parenthesis as a second and third string parameter to the function. 

Here goes the code: 



private static bool Balanced(string input, string openParenthesis, string closedParenthesis)
        {
            
            try
            {
                if (input.Length > 0)
                {
                    //Obtain first character
                    string firstString = input.Substring(0, 1);

                    //Check if it is open parenthesis
                    //If it is open parenthesis push it to stack
                    //If it is closed parenthesis pop it
                    if (firstString == openParenthesis)
                        stack.Push(firstString);
                    else if (firstString == closedParenthesis)
                        stack.Pop();

                    //In next iteration, chop off first string so that it can iterate recursively through rest of the string
                    input = input.Substring(1, input.Length - 1);
                   Balanced(input, openParenthesis, closedParenthesis);   //this call makes the function recursive
                }

                if (stack.Count == 0 && !exception)
                    isBalanced = true;
            }
            catch (Exception ex)
            {
                exception = true;
            }

            return isBalanced;
        }


Lets call this function to prove the test cases provided by the question:

  1. Balanced("(if (any? y) sum (/1 y))", "(",  ")") 
  2. Balanced("I said (it's not (yet) complete). (she didn't listen)", "(",  ")") 
  3. Balanced(":-)", "(",  ")") 
  4. Balanced("())(", "(",  ")")  
Output:
  1. true
  2. true
  3. false
  4. false

   Good Luck

Tuesday, May 14, 2013

Send a test email from SharePoint


Hi Friends,

You might be working with email related activities in SharePoint like workflows or alerts in lists and libraries but before that you need to be sure that SharePoint is sending email. You can check it in numerous ways but be sure that Outgoing Email settings is configured properly in Central Administration Site.

Once you are pretty sure that Outgoing Email settings is properly configured it, you can test it on many ways : like adding users to a new SharePoint group. You can also check it via a power shell script as shown below.

_________________________________________________________________________________
$email = "merin@sprealm.com"
$subject = "Life is good"
$body = "Life is good. Be happy."

$site = New-Object Microsoft.SharePoint.SPSite "http://sprealm:5050"
$web = $site.OpenWeb()
[Microsoft.SharePoint.Utilities.SPUtility]::SendEmail($web,0,0,$email,$subject,$body)
_________________________________________________________________________________

Copy the above script in a note pad and save it in a drive with some names like SendEmail.ps1
Then run the script.
Email sent successfully returns true whereas failure returns false.

// A True or False will confirm the message has been sent or not

Reference:http://jeffreypaarhuis.com/2013/02/12/send-test-email-from-sharepoint/

Monday, May 13, 2013

Accessing Objects and Arrays in JavaScript


Hi friends, I have illustrated the way of accessing an object and array in JavaScript using for loop.

We have an object variable called "info"


 var info = {
            "full_name": "Tchelen Lilian Costa Nakarmi",
            "title": "Staff Author",
            "links": [
                { "blog": "http://tchelennakarmi.blogspot.com" },
                { "facebook": "http://facebook.com" },
                { "google": "www.google.com" },
                { "twitter": "http://twitter.com/" },
                { "youtube": "http://www.youtube.com/" }
                ]
        };

It has three properties as shown below.
  1. full_name:  is an object
  2. title: is an object
  3. links: an array of object
We use indexed for loop to traverse through properties of "info" object 
 for (var i = 0; i < info.links.length; i++) {
................................
}
We use iterative for loop to traverse through properties of objects inside "links" array.
for (key in info.links[i]) {
...................................
}

The traversed items, viz. key value pair of object is appended to a string called output in a form of ordered list item. And we finally assign this output string to the innerHTML of the order list. 

Below is the full code and output. 

_________________________________________________________________________________

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Accessing Objects</title>
</head>
<body>
    <h2>Links</h2>

    <ol id="links">

    </ol>

    <script>
        var info = {
            "full_name": "Ray Villalobos",
            "title": "Staff Author",
            "links": [
                { "blog": "http://iviewsource.com" },
                { "facebook": "http://facebook.com/iviewsource" },
                { "podcast": "http://feeds.feedburner.com/authoredcontent" },
                { "twitter": "http://twitter.com/planetoftheweb" },
                { "youtube": "http://www.youtube.com/planetoftheweb" }
                ]
        };

        var output = '';

        for (var i = 0; i < info.links.length; i++) {
            for (key in info.links[i]) {
                if (info.links[i].hasOwnProperty(key)) {
                    output += '<li>' +
                        '<a href="' + info.links[i][key] + '">' + key + '</a>' +
                        '</li>';
                }
            }   //for each object
        }       //for each array

        var update = document.getElementById('links');
        update.innerHTML = output;
    </script>

</body>
</html>
________________________________________________________________________________


A simple table in Windows Store App

Hi friends, a table or datagrid is deprecated in Windows 8 App but I desperately needed that in my app. The GridView control in Windows App does not have notion of Rows or Columns and neither does a ListView.

However I have created a simple table using a ListView and have hardcoded the List Items. Will be doing it dynamically soon.

Here goes my XAML code.

<ListView Grid.Row="0" HorizontalAlignment="Center" Width="300" Margin="0,20,0,0 ">
            <ListViewItem>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Width="150">Apple</TextBlock>
                    <TextBlock>100</TextBlock>
                </StackPanel>
            </ListViewItem>
            <ListViewItem>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Width="150">Banana</TextBlock>
                    <TextBlock>2000</TextBlock>
                </StackPanel>
            </ListViewItem>
            <ListViewItem>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Width="150">Oranges</TextBlock>
                    <TextBlock>1500</TextBlock>
                </StackPanel>
            </ListViewItem>           
        </ListView>

And here is how it looks like.