Wednesday, June 27, 2012

Delete all items from SharePoint list programmatically

In this article we will discuss how to delete all items programmatically by using SharePoint object model. Till now if you are thinking you will call the delete() method, then for sure it is not going to work. You can also check my last article on Add Retrieve SharePoint list items using Object Model.

Suppose you want to delete all items from the SharePoint List once the item count greater than 2000. If you will simply call the Delete() method in a for each statement  like below then it is not going to work, because the itemcount will change whenever you will delete one item. So after that it will delete one item at a time.

Wrong Approach:

 void DeleteListItems()
        {
            SPSite myTopSite = SPContext.Current.Site;
            {
                SPWeb myTopWeb = SPContext.Current.Site.RootWeb;
                {
                    SPList list = myTopWeb.Lists.TryGetList("MyCustomList");

                    if (list != null)
                    {
                        if (list.ItemCount > 2000)
                        {
                            foreach (SPListItem item in list.Items)
                            {
                                item.Delete();
                            }
                        }
                    }
                }
            }
        }

If you want to know more about TryGetList, then you can read a very good article on TryGetList here.

-->
Correct Approach:

 void DeleteListItems()
        {
            SPSite myTopSite = SPContext.Current.Site;
            {
                SPWeb myTopWeb = SPContext.Current.Site.RootWeb;
                {
                    SPList list = myTopWeb.Lists.TryGetList("MyCustomList");
                    System.Collections.Hashtable htItems = new System.Collections.Hashtable(list.ItemCount);
                    if (list != null)
                    {
                        if (list.ItemCount > 2000)
                        {
                            foreach (SPListItem item in list.Items)
                            {
                                //item.Delete();
                                htItems.Add(item.ID, null);

                                foreach (int ID in htItems.Keys)
                                    list.Items.DeleteItemById(ID);

                                htItems.Clear();
                            }
                        }
                    }
                }
            }
        }

Here rather deleting the item directly, we are keeping the item id's in a Hash table and then we are deleting the list item based on the ID.

Tuesday, June 26, 2012

Deploy assembly to GlobalAssemblyCache or Web Application in SharePoint 2010

Introduction:
In this article we will discuss how to deploy an assembly to GlobalAssemblyCache or Web
Application Bin folder in SharePoint 2010.

Description:
Suppose we need to deploy a Visual WebPart to SharePoint 2010 which internally refering to a 3rd part dll. Then we need to add the assembly in the package before making the .wsp file.

Request for the permission of type 'Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' failed

Error:
I got this error while trying to deploy a visual webpart to SharePoint 2010. The deployment type is of web application rather globalassemblycache. The error message I got is Request for the permission of type

'Microsoft.SharePoint.Security.SharePointPermission, Microsoft.SharePoint.Security, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' failed. You can also check lots of errors and solutions.

Saturday, June 23, 2012

Add Retrieve SharePoint list items using Object Model

In this article we will discuss how to add and retrieve list items using SharePoint 2010 object model. You can also check my last article on Client Object Model SharePoint 2010Also check how to Create List and Add Item to that List using SharePoint 2010 client object model?

Before adding an item to a SharePoint List, you can follow an article on how to create SharePoint 2010 list.  But if you want to create list using SharePoint Designer then you can visit this article

Friday, June 22, 2012

Site Pages and Site Assets libraries link missing in SharePoint 2010

In this article we will discuss about the missing Site Pages and Site Assets libraries link in SharePoint 2010. Also you can check my last article on Hide Title column from SharePoint List . Also you can see the difference between Site Pages and Application Pages in SharePoint 2010.

Recently I came across a situation, In a publishing site If I go to Site Actions -> View All Site Content.

Inside the Document Libraries section I did not found the link for Site Assets and Site Pages. But the strange thing I noticed that the links are visible in other publishing sites inside the site collections.

Solution:
A very simple solution to work with it. Go to Site Actions -> Site Settings and then from the Site Setting page go to the Site Settings section and click on Manage Site Features as shown in the figure below.
It will Open the Features page that contains all the Feature available for the particular site.
Then Find the Feature "Wiki Page Home Page" and Activate that as shown in the figure.


Once You will activate then the Site Pages and Site Assets libraries links will be available in the View All Site Content page. Once it is available you can deactivate the Wiki Page Home Page feature, it will not disappear again.

Wednesday, June 20, 2012

Install or UnInstall Assembly dll into GAC using PowerShell

Introduction:
In this article we will discuss how to install and uninstall an assembly dll into GAC using PowerShell commands. Also you can check my last article on Some SharePoint 2010 PowerShell commands.

Description:
Suppose you have a dll and you want to put  it inside the GAC (C:\Windows\assembly), then you have to do this using commands. Because direct copy paste is not allowed there. So the better approach is to use Gacutil tool. To put a dll into GAC, the dll should be strongly named.

Sunday, June 17, 2012

Upgrade solution in SharePoint 2010

In this article we will discuss how to upgrade solution in SharePoint 2010 using PowerShell or STSADM. Also you can check my last article on Powershell command to install activate feature SharePoint 2010.

Suppose you have deployed one solution and after that you have modified the code then it is better to upgrade the solution rather than remove the solution and deploy the solution again.

Here is the STSADM command to upgrade solution:

Saturday, June 16, 2012

Completed SharePoint 2010 Certification : 70-573

Today (16th June 2012) I have completed Microsoft SharePoint 2010, Application Development (70-573). I was preparing for this paper from last few days and today I took the chance in NIIT kormangala center. Good news is I cleared the exam but bad news is very low score.

Friday, June 15, 2012

Working with SharePoint Client Object Model

In this article we will see some sample codes to work with websites using SharePoint client object model. You can aslo see my last article on how to create list using object model in SharePoint.

To communicate with a SharePoint 2010 website using client object model we need to use the class ClientContext. This class takes the site url as a parameter to the constructor.

In the below demo we will see how to retrieve website properties like Title, Descritpion etc using client object model.

To use client object model we have to write the using statement.

Friday, June 1, 2012

Display or Hide Quick Launch SharePoint 2010

In this article we will check how to show or hide quick launch programmatically using SharePoint 2010 object model. In my last article we have checked Causes of Performance issues in SharePoint 2010.

Quick launch usually appears in the left side of a web site as shown in the figure below:
Display or Hide Quick Launch through Browser: