Monday, 7 October 2013

Customize Post.aspx Without Error

In order to customize SharePoint blog's Post.aspx page, you need to convert it into XSLT Data View using SharePoint designer. But, the problem is when you try to convert it, the below error occurs:

"Failed setting processor stylesheet: Expression expected. -->=<--@Author"


Solution:

  1. In your SharePoint blog site, go to Post.aspx.
  2. Click Site Actions >> Edit Page
  3. Delete Post webpart
  4. Add new Post webpart.
  5. Exit edit mode.
  6. Open Post.aspx using Sharepoint Designer
  7. Right-click Post webpart and then convert to XSLT Data View. (You should now be able to convert it without error)
  8. In Common Data View Tasks >> Parameters >> add new parameter ID (Parameter source = Query String; Query String Variable = ID)
  9. In Common Data View Tasks >> Filter >> add filter ID Equals [ID]. 

After that, you can now edit the default display page of a SharePoint blog site based on your requirements.




Saturday, 5 October 2013

Restore Web Application By Using Content Database Backup

Below are the steps on how to restore a SharePoint web application by using the content database backup. (e.g. Testing2 is the name of content database backup)

  1. Go to the SharePoint central administration and create new web application with the same name & database name that you will restore.
  2. In Application Management >> Content databases, select your newly created web application and then delete the newly created content db (remove content database)
  3. In Sql Server, restore your content database backup. If there's an error saying that it's currently in use, just wait for 3 to 5 mins and then try again.
  4. In Application Management >> Content databases, add a content database. Set the database name equal to the restored content db in step 3. (for this case, it's Testing2)



Get User Profile Picture URL Using JQuery

I encountered an issue while working on a discussion board in SharePoint 2007. The issue is the user's profile picture is not displaying in the flat view of the discussion board even though there is a picture associated with the user profile. Instead the picture from this url "_layouts/images/person.gif" is displaying. So in order to fix this issue, I just used jQuery.

Here is the sample script on how to get the user profile picture url through jQuery:

function spjs_QueryPicURL(itm){
var pic=null;

var query = "<Query><Where><Eq><FieldRef Name='Title' /><Value Type='Text'>"+itm+"</Value></Eq></Where></Query>";
$().SPServices({
    operation: "GetListItems",  
    async: false,
    listName: "6ccdab10-1068-4d14-8f72-2a55f02938da",
CAMLQuery: query,
    CAMLViewFields: "<ViewFields><FieldRef Name='Picture' /></ViewFields>",  
    completefunc: function (xData, Status) {    
      $(xData.responseXML).find("[nodeName='z:row']").each(function() {
        pic = $(this).attr("ows_Picture");
      });
    }
  });
 
if(pic == 'undefined')
pic=null;

return pic;
}


**** Note:

listName is the list ID of User Information List

****************


Thursday, 3 October 2013

Remove Workflow Column from SharePoint List View

When you attach a workflow to a SharePoint list, the workflow column will be added automatically to the default view of the list. This column shows the status of the workflow and when you click on the status, you will be redirected to the Workflow Status page. Usually, this information is of no practical use for end users therefore you don't want this column to appear in the list view. So in order to achieve it, you need to write a custom code that will remove the workflow column programmatically.


Below is the sample code of a console application that removes workflow column from the default view of the SharePoint list:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace RemoveWorkFlowColumnFromSPView
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 4)
            {
                Console.WriteLine("Please input parameters.");
                Console.WriteLine("Format: <site url> <list name> <view name> <workflow template name>");
                return;
            }

            string workflowColName = string.Empty;
            string siteUrl = args[0]; // "http://www.vpc.com/sites/PFW"; //
            string listName = args[1]; // "Perimeter Firewall Request"; //
            string viewName = args[2]; // "All Requests"; //
            string workflowName = args[3]; // "FirewallRequestWorkFlow"; //


            using (SPSite site = new SPSite(siteUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;

                    SPView objView = web.Lists[listName].Views[viewName];

                    //static name of the workflow column is equal to 8_character_name_of_workflow_column,
                    // without space and is case sensitive.
                    workflowColName = workflowName.Substring(0, 8);

                    if (objView.ViewFields.Exists(workflowColName))
                    {
                        objView.ViewFields.Delete(workflowColName);
                        objView.Update();

                        Console.WriteLine(string.Format("{0} column: {1} has been removed successfully from the {2} view",
                            workflowName,
                            workflowColName,
                            viewName));
                    }

                   

                    web.AllowUnsafeUpdates = false;

                   
                }
            }

           

        }
    }
}





Wednesday, 2 October 2013

Change Default Form Per Content Type

By default in SharePoint list, the following things happen:

  • When you click New Item, you will be redirected to /NewForm.aspx page.
  • When you click Edit Item, you will be redirected to /EditForm.aspx page.
  • When you click View Item, you will be redirected to /DispForm.aspx page.

If you don't want to use these default forms and you want to use a different form, you need to change the default form per content type of the SharePoint list. Basically, you can do it by using SharePoint designer but sometimes it is not working that's why I prefer doing it programmatically. And since I'm doing it always, I already created a console application (exe program) in order to change it via code.


The console application requires five input parameters as listed below:
  1. Full URL of the site
  2.          e.g. http://www.xc.com/sites/domain
  3. Name of the SharePoint list
  4.          e.g. Domain Account Request Application
  5. Name of the content type
  6.          e.g. Request
  7. Type of the form that you want to change. (You need to input here either new, edit or display.)
  8.          e.g. new
  9. Name of the different form
  10.          e.g. NewForm2.aspx


Here is the code that I used:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace ChangeDefaultFormPerContentType
{
    class Program
    {
        static void Main(string[] args)
        {

            if (args.Length != 5)
            {
                Console.WriteLine("Incomplete parameter");
                Console.WriteLine("Input the parameters. Format: <full site url> <listName> <content type Name> <Form Type: new | edit | display> <Form Name>");
                Console.WriteLine("e.g. http://www.abc.com/sites/Firewall Perimeter Request new NewForm2.aspx");
                return;
            }

            string siteUrl = args[0];  // "http://www.xc.com/sites/domain"; //
            string listName = args[1]; // "Domain Account Request Application"; //
            string contenType = args[2]; // "Request"; //
            string formType = args[3]; // "new"; //
            string formName = args[4]; // "NewForm2.aspx"; //

            using (SPSite site = new SPSite(siteUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    SPList list = web.Lists[listName.Trim()];
                    string sz = list.RootFolder.Url;
                    SPContentType ct = list.ContentTypes[contenType.Trim()];

                    switch (formType.Trim())
                    {
                        case "new":
                            {
                                ct.NewFormUrl = sz + "/" + formName.Trim();
                                ct.Update();
                                list.Update();
                                Console.WriteLine(string.Format("NewFormUrl of {0} content type was updated successfully into {1}", contenType, formName));
                                break;
                            }
                        case "edit":
                            {
                                ct.EditFormUrl = sz + "/" + formName.Trim();
                                ct.Update();
                                list.Update();
                                Console.WriteLine(string.Format("EditFormUrl of {0} content type was updated successfully into {1}", contenType, formName));
                                break;
                            }
                        case "display":
                            {
                                ct.DisplayFormUrl = sz + "/" + formName.Trim();
                                ct.Update();
                                list.Update();
                                Console.WriteLine(string.Format("DisplayFormUrl of {0} content type was updated successfully into {1}", contenType, formName));
                                break;
                            }
                    }


                }
            }

        }
    }
}



Tuesday, 1 October 2013

Shrink Database Logs

Shrinking the transaction log file is a process of reducing the size of the transaction log. This process is important because if the transaction log file is left unchecked it can grow until it consumes the hard disk space of your server.

Listed below are the steps on how to truncate or shrink database log files (LDF) in your Development or Local Servers. In these steps we will set the database into a Simple Recovery Model which automatically reclaims log space. 
  1. Right click database >> Options >> Set Recovery Model to Simple >> OK
  2. Right click database >> Tasks >> Shrink >> Files
  3. Select File type = Log >> OK
  4. Set back the Recovery Model to Full

For more information about recovery model refer to http://msdn.microsoft.com/en-us/library/ms189275.aspx




Error - The site collection could not be restored

I encountered this error while restoring a site collection in a separate content database in another server. The complete error message is:

"The site collection could not be restored. If this problem persists, please make sure the content database are available and have sufficient free space."

Below are the steps that I've done to debug and fix the issue:

  1. I checked the size of the hard disk drives and it has sufficient free space. 
  2. I tried to restore it in another PC / server and I have restored it successfully. (That's why I wondered why this error happens only in that particular server)
  3. I checked the design of dbo.Sites table in the newly created content db and found out that there are two missing columns, namely FullUrl and UserAccountDirectoryPath. So, I added manually these columns and after that I have successfully restored the site collection in that server.

FullUrl column


UserAccountDirectoryPath column