Posted by: R Manimaran | January 1, 2012

2011 in review

The WordPress.com stats helper monkeys prepared a 2011 annual report for this blog.

Here’s an excerpt:

Madison Square Garden can seat 20,000 people for a concert. This blog was viewed about 61,000 times in 2011. If it were a concert at Madison Square Garden, it would take about 3 sold-out performances for that many people to see it.

Click here to see the complete report.

Posted by: R Manimaran | December 31, 2011

Happy New Year 2012

Posted by: R Manimaran | August 9, 2011

Missing Documents Opened from Outlook Attachment and edited

Missing Documents Opened from Outlook Attachment and edited

Recently I opened a word document directly from the Outlook mail attachment and do editing. I have enabled the auto save option also. Other than that I used to save the file now and then while editing. On completing my changes I closed the file. After closing only I realized that I have opened the file directly from the Outlook and did those modifications. I again go to the mail which contains that file and re-opened. I found my changes are missing. In the recent documents of the word also the file is missing.

I do a windows search with the modification I have did in that file. But I can’t able to find the file. I thought that my one day work is gone and again I need to re-work on that document.

But I have the confident that somewhere in the system my file will be there. Finally I found that in the following location.

All the files opened from edited and saved from the outlook attachments get saved under this folder. This Content.Outlook is the hidden folder. After Temporary Internet Files type the Content.Outlook in the address bar to open the folder.

Inside the Content.Outlook you will have the folder similar to the above folder name. All the files will be within that folder.

Moral Learned: Though I found the document, the one I learned is don’t edit the document directly from the outlook attachment. First copy to your working folder and do the change. J

SharePoint: Join Query using Lambda Expression in SharePoint List

Join: Join is the extension method which works on the IEnumerable

I found many articles and examples which uses LINQ to join two tables. I tried the same using Lambda Expression. For tables I have used SharePoint List. The Join method operates on the SPListItem and Join two SharePoint List.

I have created two SharePoint List

· Test

· Countries

Test


Countries

Here “Test” List contains a Lookup column (Country) which refers the “Countries” List.


SPList myList = oSPWeb.Lists.Cast()

.FirstOrDefault(l => l.Title == "Test");

SPList myCountryList = oSPWeb.Lists.Cast()

.FirstOrDefault(l => l.Title == "Countries");

var items = myList.Items.Cast().Join

(myCountryList.Items.Cast(),

(l1 => ReturnLookupValue(Convert.ToString(l1["Country"]))),

(l2 => Convert.ToString(l2["Title"])),

((l1, l2) => new { l1, l2 }));

foreach (var item in items)

{

Console.WriteLine(item.l1.Title.ToString() +"-"+ item.l2.Title);

}

Here in the above I am using the columns “Title” from Countries List and “Country” Column from the Test List. As the Lookup value will return the value along with the LookupId(#1;India), I removed the ID using the method ReturnLookupVale.

ReturnLookupValue method will contain the following code to return the lookup value.


SPFieldLookupValue lookup = new SPFieldLookupValue(LookupValue);

return lookup.LookupValue;

ToDictionary: It an LINQ extension method which stores the data as Key Value pairs. Here in the below I am fetching the Email Id and Title from a SharePoint List as a Key Value pairs.

Get the List using Lambda expression. If the specified list is not found, then it will return null.

SPList myList = oSPWeb.Lists.Cast<SPList>()
    .FirstOrDefault(l => l.Title == "ExternalPeople");

Get the Email and Tilte as key value pair. Before fetching do an filter operation where the Qualification is “BE”

IDictionary<string,string> NameswithEmail = myList.Items.Cast<SPListItem>()                                                                                .Where(itm => Convert.ToString(itm["Qualification"]) == "BE")                                                                                .ToDictionary(itm => Convert.ToString(itm["Email"]),
  itm => Convert.ToString(itm["Title"]));

Print the Key-value pairs using foreach

foreach (KeyValuePair<string, string> names in NameswithEmail)
{
    Console.WriteLine("Email:"+names.Key);
    Console.WriteLine("First Name:"+names.Value);
}

ToLookup: Allow you to specify Key and Values as TODictionary. The difference between ToLookup to ToDictionary is the former allows duplicates Keys while the latter does not.

Another difference is Lookups are immutable. i..e once you create a lookup you cannot add or remove elements from it.

In the above Dictionary collection NameswithEmail we have methods called Add and Remove. But we will not have any such methods in ILookup collection.

var myLookupItems = myList.Items.Cast<SPListItem>().ToLookup(itm => Convert.ToString(itm["Qualification"]), itm => itm);
var EmployeesBE = myLookupItems["MCA"];
 foreach (var str in EmployeesBE)
 {
   Console.WriteLine(str.Title);
 }

Instead of Var we can use the ILookup Enumerable as below

ILookup<string,SPListItem> myLookupItems1 = myList.Items.Cast<SPListItem>()

.ToLookup(itm => Convert.ToString(itm["Qualification"]),

itm => itm);

Hidding and Restoring Views Dropdown in the SharePoint 2007 List using OOTB

My team-mate asked a questions that he wants to hide the Views dropdown in the SharePoint List. But the condition is that we should not use the coding (any JavaScript or Object model).

Following is the output he likes to have.

I tried with all the settings and Permissions (Manage Personal Views). But no result. After a long search, I found this. It will be useful to someone.

Steps:

1. Click the List link in the Quick Launch.

2. In the Opened page, select Site Actions àEdit Page

3. Click Add a Web Part.

4. In the Add web Part popup select your List to add as a Web Part.

5. Click Exit EditMode.

6. Now in the Page you will have two webpart. One with View DropDown and another one without the dropdown.

7. Now Edit the page and Close the below webpart which contains the View dropdown.

8. Now the expected result will be arrived.

To bring back the Views Drop down:

To bring back the Views drop down we need to follow some set of steps as below.

1. In the List select Settings à Create View.

2. Click “All Items” view from Start from an existing view.

3. Provide the View Name as “All” and check the “Make this the default View”.

4. Click ok and create the view.

5. Now the page will have an extra webpart added and look like this.

6. Now again edit the page and delete the webpart one which does not have the drop down.

7. Now the page will have the list with views drop down.

8. Then I changed the View from “All” to “All Items”. My DropDown again went off. To Correct this we need to have the help of SharePoint Designer.

1. Open the SharePoint Site in SharePoint Designer.

2. Go to List and select the List that you are currently working.

3. You will have the following following aspx pages.

a. AllItems.aspx

b. All.aspx (The view you created based on AllItems)

c. DispForm.aspx

d. EditForm.aspx

e. NewForm.aspx

4. Now Delete the AllItems.aspx. Before Deleting you have a copy of this view.(Here we have All.aspx which is the copy of All Items view. If you do not have the create a view on the page based on All Items view).

5. Now go to SharePoint site in browser and create a new View based on view name “All” which we created earlier.

6. Set that View as default view. Now we got back the View drop down.

7. Now the another problem. The view you have deleted i..e All Items is set default before you may lose the link in Quick launch.

To Restore the Quick launch link.

1. Go the List and select List Settings.

2. Click Title,Description and Navigation link under General Settings.

3. Under Navigation select Yes checkbox on “Display this list on the Qucik Launch”.

4. Now the List’s link will appear in Qucik launch.

Lambda Expression contains lot of extended methods which works on collection. Here I have tried Select and Where method to fetch items from the SharePoint List.

SharePoint List :

Selecting Single value:

using (SPSite oSPSite = new SPSite("http://Moss2007Server"))
{
 using (SPWeb oSPWeb = oSPSite.OpenWeb())
    {
     SPList myList = oSPWeb.Lists
                           .Cast<SPList>()
                           .FirstOrDefault(l => 
                      Convert.ToString(l.Title) == "ExternalPeople");

if (myList != null)
       {
        var names = myList.Items.Cast<SPListItem>()
         .Where(itm => Convert.ToString(itm["Qualification"]) == "BE")                                                                                
         .Select(itm => Convert.ToString(itm["Title"]));
                                
        foreach (string str in names)
         {
           Console.WriteLine(str);
         }
 }
    }
}

Here .Select returns IEnumerable collection which can be Iterated using foreach
If we want .Select to return the SPListItem object then the statement should be like this.

Selecting Object:

var namesSPListItem = myList.Items.Cast<SPListItem>()
            .Where(itm => Convert.ToString(itm["Qualification"]) == "BE")                                                                               
                     .Select(itm => itm);                        
Console.WriteLine("************** Return as SPListItem **************");
foreach (SPListItem item in namesSPListItem)
{
   Console.WriteLine("First Name:"+item["Title"]);
   Console.WriteLine("Last Name:"+item["Last Name"]);
}

In the single value selection, we did not know what str is containing (whether FirstName or LastName). To have some readable and easily understandale we can change the above to use new operator along with .Select as below

var items = myList.Items
.Cast<SPListItem>()
.Where(itm => Convert.ToString(itm["Qualification"]) == "BE")
            .Select(n => 
                 new {
                        FirstName = Convert.ToString(n["Title"]),
                        LastName  =Convert.ToString(n["Last Name"])
                       }
  );

So in the Foreach we can use the above variables as below.
foreach (var item in items)
{
  Console.WriteLine("First Name:"+item.FirstName);
  Console.WriteLine("Last Name:"+item.LastName);
}

LINQ has a option to declare a temparory variable using let. This can be done in Lambda expression as below. In the following I would like to concat the FirstName, LastName and return as FullName.
var items1 = myList.Items.Cast<SPListItem>()
               .Where(itm => Convert.ToString(itm["Qualification"]) =="BE")
                .Select(n => 
             new {
                   FullName=string.Concat(n["Title"]," ",n["Last  Name"])
                 }
    );

Posted by: R Manimaran | July 11, 2011

Microsoft Community Contributor Award 2011

Hi all,

I happy to share that my contributions to the MSDN forums is recognized by Microsoft and honored me with the award “Microsoft Community Contributor 2011″. Thanks to Microsoft Online  support & community and the one who referred me.

I really like to thank Aravind who encouraged me and helped to achieve this award.

Selecting Random ListItems

I have a list with the following list items. I need to select the records in the list on a Random fashion. I have tried in Lambda and get it done. It will be useful to someone.

SPList myList = oSPWeb.Lists.Cast()
                            .FirstOrDefault(l => l.Title == "Test");

 if (myList != null)
  {
  SPQuery query = new SPQuery();
  Random rnd = new Random();
  IOrderedEnumerable<SPListItem> Items = myList.GetItems(query)
                                             .Cast(<SPListItem>)
                                             .AsEnumerable()
                                             .OrderBy((i => rnd.Next()));

         foreach (SPListItem item in Items)
         {
           Console.WriteLine(item[SPBuiltInFieldId.Title].ToString());
         }
}

The Output will be as below

Posted by: R Manimaran | May 31, 2011

SharePoint 2007: Delete Quick Launch nodes recursively

In SharePoint 2007, I have Navigation as shown below. I need to delete all the navigation recursively.

I here wrote a utility method which will delete all the nodes (both Heading and child Nodes).


public static void DeleteNavigation()

{

using (SPSite site = new SPSite("http://maran"))

{

using (SPWeb web = site.OpenWeb())

{

SPNavigationNodeCollection collec = web.Navigation.QuickLaunch;

for (int i = collec.Count - 1; i >0; i--)

{

int cntChildren = collec[i].Children.Count;

DeleteNavigationRecursively(collec[i]);

if (cntChildren > 0)

{

//Delete the Root Node

Console.WriteLine("Deleting Node:"+collec[i].Title);

collec[i].Delete();

}

}

}

}

}

private static void DeleteNavigationRecursively(SPNavigationNode node)

{

if (node.Children.Count == 0)

{

Console.WriteLine("Deleting Node:" + node.Title);

node.Delete();

}

else

{

for (int j = node.Children.Count - 1; j >= 0; j--)

{

DeleteNavigationRecursively(node.Children[j]);

}

}

}

Call the method DeleteNavigation will delete all the nodes recursively.

Like this at Facebook!

Older Posts »

Categories

Follow

Get every new post delivered to your Inbox.