Tuesday, October 29, 2013

Redirect to Another Page On Clicking OK in JavaScript Alertbox

Page Redirection after client script does not show the popup or alert box added to the code using ClientScript.RegisterStartupScript 

Example:

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Operation Completed')", true);

 Response.Redirect("~/default.aspx"); 


In this code you will not get the alertbox and will be simply redirected to default.aspx page.

Modify the code like below

this.Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", "alert('Operation Completed.');window.location='default.aspx';", true);  

This will show the alertbox and upon clicking OK you will be redirected to default.aspx page                  

Thursday, October 24, 2013

SharePoint Storage Metrics


Problem

Are you having a quota limit exceeded problem and you want to know which site or list/library is consuming all the storage. Increasing the quota does not help? 

Resolution:

Storage Metrics

You can trace the sites that are using maximum storage on the server.
Storage Metrics provide a summarized report of how storage for the site collection is being used across Site Collections and Sites. This helps administrators and owners to quickly identify content that can be archived or removed in order to remain within their allocated quotas.
To begin using Storage Metrics select Site Actions, and then click Site Settings.
From Site Settings select Storage Metrics under Site Collection Administration or simply enter the following URL
http://siteColl/_layouts/storman.aspx 
 It will show complete report of the site usage.
 You can drill-down to building block level by clicking the title of the items in the report.

Storage Metrics shows wrong size of the list item or Document

The storage metrics shows a huge size of the documents ( that is not the actual size) due to its previous versions that are stored in its Content Database. 
As an example http://siteColl//Documents/abc.pptx this file has an actual size of 55.6 MB  and has 300 versions available so it takes a total quota size of 300 x 55.6 MB data. 
If this is the issue with a single document library or a list or lets say a few of them you can simply limit the maximum versions according to need (I set it to 5) for that library. But we actually need a permanent solution for the issue as there are a lot of documents available. 
A hotfix from Microsoft is available on  http://support.microsoft.com/kb/2687339  Apply this hotfix on your server. Or you can write custom code in Powershell or Server Object model to update the maximum version of the libraries or lists to desired numbers and updating all the list items.


Limiting the versions of document libraries and list while enabling versioning is a huge plus.




Wednesday, October 23, 2013

IE Crash When Adding a new List Item in to a List in Datasheet View

I came across an issue from one of the clients regarding Internet Explorer crash when user tried to a new list item in to a custom list. This list had many fields of around all field types including four to five Person or Group fields.

Reported Problem:
1. Can attach documents to existing rows, but cannot add a new row for a new item without Internet Explorer freezing. 
a. Click Debug and receive an “Unhandled win32 exception in iexplore.exe (10940)” error message
How it happens:
1. Either click New to add a new row in the Datasheet view, or go to the bottom of the datasheet and start populating fields in a blank row.
After populating all fields, and tabbing, the progress (activity) indicator above the clock will change from blank to green bars going back and forth. THEN, Internet explorer returns an error message, “Unhandled win32 exception in iexplore.exe (10940)”.

Resolution: 

SharePoint 2010 List Datasheet View has a known bug of this type, Microsoft  provides a hotfix (Click for Official Link to the Hotfix) named " Office 2010 hotfix package (Stslist-x-none.msp): October 30, 2012" for this issue. Download and update your server for the issue if it occurs for all lists. 

In my case, a similar list worked fine created from its template. The problem was just with the list. The last solution was to create a backup for the list and check all the field that possibly could cause the Problem 

Delete all the fields with Calculated field type and Person and Group field type  one by one and try testing the list again,I found out that a Person or Group field was responsible for the issue, I recreated it, stopped related workflow, cleared browser Cache but it was none of my use.

At last I found out that the field allowed multiple user selection, disallowing multiple selection (not needed in my case) resolved the issue.

I hope this helps :)





Tuesday, October 22, 2013

Creating a lookup field in SharePoint List Programmatically


        private void CreateList(SPWeb web)
        {
            SPListCollection lists = web.Lists;
            SPList list = lists.TryGetList("Child List");
            if (list == null)
            {
                lists.Add("Child List", "Example child list "  , SPListTemplateType.GenericList);
                list = web.Lists["Child List"];
                list.OnQuickLaunch = true;

          #region CreatingListFields
            if (list != null)
            {
                SPFieldText txtField = null;

                // Rename Title Field
                txtField = (SPFieldText)list.Fields["Title"];
                if (txtField != null)
                {
                    txtField.Title = "Text Field";
                    txtField.Update();
                }

               // Creating a lookup field
               SPList targetList= web.Lists.TryGetList("Parent List");
               if (targetList != null)
               {
                   list.Fields.AddLookup("Child Lookup Field Name", targetList.ID, false);
                   SPFieldLookup lookup = (SPFieldLookup)list.Fields["Child Lookup Field Name"];
                   lookup.LookupField = targetList.Fields["ParentListFieldName"].InternalName;
                   lookup.AllowMultipleValues = false;
                   lookup.Update();
               }             

                list.Update();
            #endregion

                // Add a default View to the List
        
            #region DefaultView 
            SPView view = list.DefaultView;
            view.RowLimit = 30;
            view.Title = "All Items";
            view.Update();
            list.Update();
            #endregion
        }
}


   

Thursday, October 3, 2013

"Cannot complete this action. Please try again" - Client Object Model

While using Client Object Model to access a list item I jumped into a strange error :) I should Say ! I went like "Cannot complete this action. Please try again" I reviewed the whole code and was blank at the end... I Googled whole day and found no option other than Thread.Sleep(toUnlimitedTime) as I tested :) At the end desperately  I reviewed my CamlQuery and there it goes!!! I missed adding </Query> ending tag in the query. And the error was gone :) Hard work Huh!!

Web web = ctx.Web;
            ctx.Load(
                web,
                website => website.Title,
                website => website.HasUniqueRoleAssignments);
            ctx.ExecuteQuery();
            List list = ctx.Web.Lists.GetByTitle("MyList");
            CamlQuery query = new CamlQuery();      
                string que = @"<View><Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>xyz</Value></Eq></Where></Query></View>";
                query.ViewXml = que;
                SP.ListItemCollection coll = list.GetItems(query);
            ctx.Load(coll, items => items.Include(item => item,                                              
                                                  item => item.Id,
                                                  item => item.HasUniqueRoleAssignments,
                                                  item => item.DisplayName));
            ctx.ExecuteQuery();