Thursday 28 June 2012

How to embed PDF and text file contents in sharepoint pages

Hi,

I was presented a requirement where some of the site administrators wanted to show contents from PDF and text files to many pages in our sharepoint sites. The content was either text or images. There is always a way to add this under the page controls and pods but it was not an effective way of doing so since for any change in the requirement it has to be carried out in all pages manually. Hence I used the content editor webpart to display the text. The file was stored in a document library and it was being referred by my content editor webpart so any changes in the file will be reflected in my pages as well. To refer the file in my content editor webpart I used the following script in source editor of my webpart :


<iframe width="560" height="345" src="FileUrl" frameborder="0" ></iframe>


Thats it and it worked out


I hope this will help you out.


Thanks,
Rahul Rashu


Thursday 14 June 2012

How to delete a timer job using powershell in sharepoint

Hi,
Recently many site admins requested me to provide an easy way to delete a timer job. A timer job can always be deleted via a non documented STSADM command as
STSADM -o deleteconfigurationobject -id guid. However this command is not recommended for production environment. Hence to resolve this issue I created a powershell script that takes only one input as the guid of the timer job and deletes it. Here is the script :

param([string]$jobid)

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")


function RahulDeleteTimerJob($jobid) {

write-host "This script will delete a timer job"
        $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
        $services = $farm.Services
        foreach($service in $services)
{

$jobdefs = $service.JobDefinitions
foreach($job in $jobdefs)
{
       if($job.Id.ToString() -eq $jobid)
{

          $job.Delete()

}

}

}

write-host "The job is deleted"

}

RahulDeleteTimerJob $jobid




I hope this will help you out.

Thanks,
Rahul Rashu

Tuesday 5 June 2012

How to Hide Columns in list forms in sharepoint without using Sharepoint designer

Hi,

Recently a requirement was posted to me where the end users wanted to hide columns in list forms without using sharepoint designer. Our site admins can do this easily using sharepoint designer but since there were many lists and requests so it was not possible to go to each and every one and carry out this task manually. Hence I created a tool using C#. This works in both MOSS 2007 and SPS 2010.This takes three inputs:
1. The complete List URL. ex: http://webApplication/Site/Lists/ListName/Allitems.aspx. (Note:- The URL upto any view can be used here).
2. The field name that is to be hidden.
3. 1 ,2 or 3 based on the requirement as these numbers stands for "1 for EditForm, 2 for DisForm and 3 for NewForm".

On successful completion it will show a message that the execution is completed.
Here is the code to do this:


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

namespace HideFieldInListFormRahul
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("This tool will hide a field from list forms. It requires List Url and field URL");
                Console.WriteLine("Please enter list URL");
                String listUrl = Console.ReadLine();
                Console.WriteLine("Please enter column name");
                String columnName = Console.ReadLine();
                Console.WriteLine("Enter 1 for EditForm, 2 for DisForm and 3 for NewForm");
                String formType = Console.ReadLine();
                using (SPSite site = new SPSite(listUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        String listRel = listUrl.Substring(web.Url.Length);
                     
                        SPList list = web.GetListFromUrl(listUrl);
                        SPField field = list.Fields[columnName];
                        if (formType.Trim().Equals("1"))
                        {
                            field.ShowInEditForm = false;

                        }
                        else if (formType.Trim().Equals("2"))
                        {
                            field.ShowInDisplayForm = false;
                        }
                        else if (formType.Trim().Equals("3"))
                        {
                            field.ShowInNewForm = false;
                        }
                        else
                        {
                            Exception ex = new Exception("No Proper number between 1 to 3 has been entered");
                            throw ex;
                        }
                        field.Update();
                        Console.WriteLine("The execution completed.Press Enter to Exit");
                        Console.ReadLine();
                    }
                }

            }

            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                Console.ReadLine();
            }
        }
    }
}

I hope this will help you out.
Thanks,
Rahul Rashu

Sunday 3 June 2012

An error was encountered while retrieving the user profile in Sharepoint 2010

Hi,

An issue was reported to me where some of the developers were facing some issues in executing the code to get user profile in sharepoint 2010. The exception was following:

Microsoft.Office.Server.UserProfiles.UserNotFoundException was unhandled
  Message=An error was encountered while retrieving the user profile.
  Source=Microsoft.Office.Server.UserProfiles
  StackTrace:
       at Microsoft.Office.Server.UserProfiles.UserProfileCache.GetUserData(UserProfileManager objManager, Nullable`1 recordId, Guid gAcct, String strAcct, Byte[] bSid, String strEmail, Boolean doNotResolveToMasterAccount)
       at Microsoft.Office.Server.UserProfiles.UserProfile.RetrieveUser(String strAcct, Guid gAcct, Byte[] bSid, Nullable`1 recordId, Boolean doNotResolveToMasterAccount, Boolean loadFullProfile)
       at Microsoft.Office.Server.UserProfiles.UserProfile..ctor(UserProfileManager objManager, String strAcct, Boolean doNotResolveToMasterAccount, Boolean forceUserIsSelf, Boolean loadFullProfile)
       at Microsoft.Office.Server.UserProfiles.UserProfileManager.GetUserProfile(String strAccountName, Boolean doNotResolveToMasterAccount, Boolean loadFullProfile)
       at Microsoft.Office.Server.UserProfiles.UserProfileManager.GetUserProfile(String strAccountName, Boolean doNotResolveToMasterAccount)
       at Microsoft.Office.Server.UserProfiles.UserProfileManager.GetUserProfile(String strAccountName)
       at WelcomeControlModification.Program.Main(String[] args)

This exception was originating from this line of code:

UserProfile userProfile = manager.GetUserProfile("Domain\userName");

We checked the domain as well as the userName and found no issues with that. We even checked the permissions of the ID executing the code. Finally a small modification based on my experience with Sharepoint 2007 solved this issue we just replaced

UserProfile userProfile = manager.GetUserProfile("Domain\userName");

With

UserProfile userProfile = manager.GetUserProfile("Domain\\userName");

In short we just added an additional \ as a escape sequence.
This resolved our issue.

I hope this will help you out.

Thanks,
Rahul Rashu