Wednesday, 30 November 2011

Can Not Open pdf files in Browser in Sharepoint 2010

Hi,

Recently someone expressed his requirements to open pdf files in browsers in the same way as excel files opens up when we use excel services. Although the settings was to open the file in web browser in advanced settings of the document library it was opening in the client application. After some digging into this issue I managed to get it done. Hence here are the steps I am sharing to get this done:

1. Open the document library where you have stored the .pdf file.
2. Go to the advanced settings and make sure that you have selected "Open in web browser" and not in client application.
3. Login to Central Administrator--> Application management --> Web Application general settings and select your web application.
4. Then select your web applicaion --> general settings.and change browser file handling to Permissive from strict as shown below















5. Click on OK and you are OK to go.

I hope this will help you out.

Thanks,
Rahul Rashu

Saturday, 26 November 2011

How to list out workflows designed by sharepoint designer in sharepoint 2007 site

Hi,
Recently I have received many requests from many site administrators that they want to track the sharepoint designer workflows created in their sites. Hence I have developed this application. This works in Sharepoint 2007. This takes an input of site url and then it enumerates each and every list and find out the workflows associated with that list. Then it checks for their base template. The base template for a SPD workflow is null since these workflows are not created with any base template. In this way I have listed out the lists and the workflows designed by SPD associated with them.


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

namespace RahulListOutSPDWorkflows
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("This tool will provide you the list of SPD workflows and asociated lists in a site");
                Console.WriteLine("Please enter the url of the site");
                String webUrl = Console.ReadLine();
                using (SPSite site = new SPSite(webUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPListCollection lists = web.Lists;
                        foreach (SPList list in lists)
                        {
                          SPWorkflowAssociationCollection wAssociations = list.WorkflowAssociations;
                          foreach (SPWorkflowAssociation wAssociation in wAssociations)
                          {
                              if (wAssociation.BaseTemplate == null)
                              {
                                  Console.WriteLine("The List " + list.Title + " has SPD workflow " + wAssociation.Name);
                              }
                          }
                        }
                    }
                }
                Console.WriteLine("Execution Completed");
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
                Console.ReadLine();
            }
           
        }
    }
}

I hope this will help you out.

Thanks,
Rahul Rashu

Saturday, 19 November 2011

The search application 'Guid' on server ServerName did not finish loading. View the event logs on the affected server for more information.

Hi,
In our new SPS 2010 environment we were trying to configure search. When we logged into the search service via central administration and clicked on content sources we got this error message "The search application 'Guid' on server ServerName did not finish loading. View the event logs on the affected server for more information." In the event viewer I was able to get this message:

Log Name:      Application
Source:        Microsoft-SharePoint Products-SharePoint Foundation
Date:          20-11-2011 01:45:35
Event ID:      6398
Task Category: Timer
Level:         Critical
Keywords:    
User:          NETWORK SERVICE
Computer:      rahul-HP
Description:
The Execute method of job definition Microsoft.Office.Server.Search.Administration.IndexingScheduleJobDefinition (Guid threw an exception. More information is included below.

The search application Guid on server ServerName did not finish loading. View the event logs on the affected server for more information.
Event Xml:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
  <System>
    <Provider Name="Microsoft-SharePoint Products-SharePoint Foundation" Guid=Guid />
    <EventID>6398</EventID>
    <Version>14</Version>
    <Level>1</Level>
    <Task>12</Task>
    <Opcode>0</Opcode>
    <Keywords>0x4000000000000000</Keywords>
    <TimeCreated SystemTime="2011-11-19T20:15:35.334377700Z" />
    <EventRecordID>48170</EventRecordID>
    <Correlation ActivityID="Guid" />
    <Execution ProcessID="2884" ThreadID="2996" />
    <Channel>Application</Channel>
    <Computer>rahul-HP</Computer>
    <Security UserID="S-1-5-20" />
  </System>
  <EventData>
    <Data Name="string0">Microsoft.Office.Server.Search.Administration.IndexingScheduleJobDefinition</Data>
    <Data Name="string1">Guid</Data>
    <Data Name="string2">The search application Guid on server ServerName did not finish loading. View the event logs on the affected server for more information.</Data>
  </EventData>
</Event>

So to come out of it I went to create a new search service instance. I went to Central Administration--> ManageServiceApplications --> new --> Search service Application.

A modal window opened as shown below:












Provide values and make sure that the account you are using as search service account must have at least read access to your web applications.

Now I was able to configure my content sources and get my items crawled.
I hope this will help you out.

Thanks,
Rahul Rashu

Sunday, 13 November 2011

How to show ID field in EditForm.aspx and DispForm.aspx in Sharepoint

Hi,

We do not get ID field in DispForm.aspx and EditForm.aspx pages out of the box. However many times business needs this field to show up in this form because of their uniqueness. To accomplish it a javascript is most easy option and it can be applied via content editor webpart. To do this follow these steps

1. Go to the list
2. Click view item with any existing item or if you do not have any item then in the URL after listname add /DispForm.aspx .
3. In the next page add ?ToolPaneView=2 . This will open the page in editable form and the add a content editor webpart. Edit it to add the HTML content.
4. Paste the below script

<script language="javascript" type="text/javascript">

_spBodyOnLoadFunctionNames.push("showID");

function showID()
{

var querystring = location.search.substring(1, location.search.length);
var ids = querystring.split("&")[0];
var id = ids.split("=")[1];
var Td1 = document.createElement("td");
Td1.className = "ms-formlabel";
Td1.innerHTML ="<h3 class ='ms-standardheader'>ID</h3>";
var Td2 = document.createElement("td");
Td2.className =  "ms-formbody";
Td2.innerHTML = id;
var Tr1 = document.createElement("tr");
Tr1.appendChild(Td1);
Tr1.appendChild(Td2);
var Location = GetSelectedElement(document.getElementById("idAttachmentsRow"),"TABLE").getElementsByTagName("TBODY")[0];
Location.insertBefore(Tr1,Location.firstChild);
}

</script>


5. save it and exit.

The same process you need to follow in case of EditForm.aspx as well. The ID field will be shown in read only mode in both the forms.

For Sharepoint 2010 content editor webparts can be edited in the following way  http://rahulrashu.blogspot.com/2011/10/how-to-add-html-content-into-content.html . You can use same script here as well but in sharepoint 2010 these forms open in form of modal windows so if you want to edit them you need to use direct URL here.

I hope this will help you out.

Thanks,
Rahul Rashu

Saturday, 12 November 2011

How to copy list permissions in Sharepoint Using Web Services

Hi All,

I have seen administrators working hard to copy permissions from one list to other list. This is a very important task from the security perspective as well. To ease this process I received various requests to develop something that will allow this via web service call since the tool will be used by site administrators and not server and farm administrators.

Hence I have created the following tool, it works in both versions of sharepoint MOSS 2007 and SPS 2010.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace RashuChangeListPermissionWebService
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("This tool will copy the permissions of a list to the other one using web services");
                RahulPerm1.Permissions sourcePermissions = new RashuChangeListPermissionWebService.RahulPerm1.Permissions();
                RahulPerm2.Permissions destinationPermissions = new RashuChangeListPermissionWebService.RahulPerm2.Permissions();
                Console.WriteLine("Please enter the url of the source site");
                String sourceSiteUrl = Console.ReadLine();
                Console.WriteLine("Please enter the name of the source list");
                String sourceListName = Console.ReadLine();
                Console.WriteLine("Please enter the url of the destination site");
                String destinationSiteUrl = Console.ReadLine();
                Console.WriteLine("Please enter the name of the destination list");
                String destinationListName = Console.ReadLine();
                sourcePermissions.UseDefaultCredentials = true;
                destinationPermissions.UseDefaultCredentials = true;
                sourcePermissions.Url = sourceSiteUrl + "/_vti_bin/Permissions.asmx";
                destinationPermissions.Url = destinationSiteUrl + "/_vti_bin/Permissions.asmx";
                XmlNode nodeSource = sourcePermissions.GetPermissionCollection(sourceListName, "List");
                XmlNode nodeDest = destinationPermissions.GetPermissionCollection(destinationListName, "List");
                XmlDocument deleteXml = new XmlDocument();
                XmlDocument createXml = new XmlDocument();
             
                XmlElement rootS = createXml.CreateElement("Permissions");
                XmlElement rootUsers = createXml.CreateElement("Users");
                XmlElement rootGroups = createXml.CreateElement("Groups");
                createXml.AppendChild(rootS);
                rootS.AppendChild(rootUsers);
                rootS.AppendChild(rootGroups);
               
                XmlNodeList nodeDLists = nodeDest.ChildNodes;
             
                String userIdentification;
                foreach (XmlNode nodeDFirst in nodeDLists)
                {
                   
                    foreach (XmlNode nodeDSecond in nodeDFirst.ChildNodes)
                    {
                        userIdentification = nodeDSecond.Attributes["MemberIsUser"].Value;
                        if(userIdentification.Equals("True"))
                        {
                            destinationPermissions.RemovePermission(destinationListName, "List", nodeDSecond.Attributes["UserLogin"].Value, "user");
                        }
                        else
                        {
                            destinationPermissions.RemovePermission(destinationListName, "List", nodeDSecond.Attributes["GroupName"].Value, "group");
                        }

                    }
                }

             
                XmlNodeList nodeListsSource = nodeSource.ChildNodes;
                XmlAttribute sourceAttribute;
                foreach (XmlNode nodeF1 in nodeListsSource)
                {
                    foreach (XmlNode nodeF2 in nodeF1.ChildNodes)
                    {
                        sourceAttribute = nodeF2.Attributes["MemberIsUser"];
                        if (sourceAttribute.Value.Equals("True"))
                        {
                            XmlElement userD = createXml.CreateElement("User");
                            userD.SetAttribute("LoginName", nodeF2.Attributes["UserLogin"].Value);
                            userD.SetAttribute("PermissionMask", nodeF2.Attributes["Mask"].Value);
                            rootUsers.AppendChild(userD);


                        }
                        else
                        {
                            XmlElement groupD = createXml.CreateElement("Group");
                            groupD.SetAttribute("GroupName", nodeF2.Attributes["GroupName"].Value);
                            groupD.SetAttribute("PermissionMask", nodeF2.Attributes["Mask"].Value);
                            rootGroups.AppendChild(groupD);
                        }
                    }
                }
                destinationPermissions.AddPermissionCollection(destinationListName, "List", createXml);
                Console.WriteLine("Execution Completed");
                Console.ReadLine();



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



There are few points about input parameters.

1. Source Site-- This will be the URL of the site where the list whose permissions we are going to copy resides. This should be only till site url and should not contain any page or list's url .
2. Destination Site- This should be also entered in the same format as above.This indicates the URL of the site that contains the list to which we are going to insert permissions after removing the existing one.

This tool first read the permissions of the destination list and then removes it one by one for all users and groups. Then it reads the permissions of the source list and then copies it to destination. It works no matter whether the source list or destination list inherit permissions from the parent site. After tool execution as expected the destination list will have its own permission values.

I hope this will help you out.

Thanks,
Rahul Rashu

Monday, 7 November 2011

Check Effective Permissions of a User in a Sharepoint Site

Hi,

One of a tedious task for site administrators is to check all permissions provided to a user. I have seen administrators checking each and every user group to find out the permissions. To minimize efforts in this direction I have created this application. This will enlist all permissions for a user at the site level. I will soon extend this for the list, libraries and even item level for more ease in this direction. I am sharing my code here it works in both Sharepoint 2007 and Sharepoint 2010. This takes 2 input values during site execution. The first one is the URL of the site where you want to check the permissions and the next one is user ID.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Collections;
using Microsoft.Office.Server.UserProfiles;
using Microsoft.Office.Server;

namespace RahulCheckSitePermission
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This tool will check the permissions of a user in the site specified");
            Console.WriteLine("Please enter the URL of the site where you want to check the permissions");
            String webUrl = Console.ReadLine();
            Console.WriteLine("Please enter the userName");
            String userName = Console.ReadLine();
            try
            {
                using (SPSite site = new SPSite(webUrl))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        ServerContext serverContext = ServerContext.GetContext(site);
                        UserProfileManager userProfileManager = new UserProfileManager(serverContext);
                        UserProfile userProfile = userProfileManager.GetUserProfile(userName);
                        String userLogin = userProfile[PropertyConstants.AccountName].Value.ToString();
                        SPUserCollection groupUsers;
                        ArrayList userInGroups = new ArrayList();
                        userInGroups.Add(userLogin);
                        SPPrincipal userPrincipal;
                        SPGroupCollection groups = web.Groups;
                        foreach (SPGroup group in groups)
                        {
                            groupUsers = group.Users;
                            foreach (SPUser groupUser in groupUsers)
                            {
                                if (groupUser.Name.Equals(userLogin))
                                {
                                    userInGroups.Add(group.Name);
                                    break;
                                }
                            }
                        }
                        SPRoleAssignmentCollection roleCollection = web.RoleAssignments;
                        foreach (SPRoleAssignment role in roleCollection)
                        {
                            userPrincipal = role.Member;
                            for (int i = 0; i < userInGroups.Count; i++)
                            {
                                if (userInGroups[i].ToString().Equals(userPrincipal.Name))
                                {
                                    SPRoleCollection roles = userPrincipal.Roles;
                                    foreach (SPRole userrole in roles)
                                    {
                                        Console.WriteLine("The user " + userLogin + " has permissions of " + userrole.Name + " given via " + userPrincipal.Name);
                                    }
                                }
                            }

                        }
                        Console.WriteLine("The execution completed");
                        Console.ReadLine();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }
        }
    }
}

The output will flash the message about each permissions. 
I hope this will help you out.

Thanks,
Rahul Rashu