Friday, June 30, 2006

SharePoint Document Puller: Extract any Word document from your SharePoint portal and save it on your hard disk!

This is a small tool developed to extract documents from your SharePoint portal. Documents extracted will be saved to your hard disk. This utility will be of great help to the administrators who use SharePoint as document management system. I have worked with such systems. I have deployed enterprise level document management systems for large organizations and I know they have got hundreds of thousands of documents to store in SharePoint libraries. Searching for specific documents in such a large document management system is a headache, so that is where this tool will come in handy. Users can search for Word documents in SharePoint. Tool will return all word documents from all sites, subsites, libraries, etc. With a single click of a button, user can save the selected document to the hard disk. It's that simple.

I could have made this tool a complicated one with lots of other features but for the sake of simplicity, I have kept it small and simple. This version has two main features:
1. With a single click, you can find all Word documents in your SharePoint deployment.
2. You can save selected document to your hard disk.
Some features that I wanted to implement and might include in the next version:
1. Currently, application retrieves database settings from a text file and hence, it is important for users to modify this text file before being able to use the application. I wanted to include a couple of fields on the form itself so that users would not have to modify the text file. Furthermore, new users may find it difficult to locate the SharePoint database and add it's name to the connection string in the text file.
2. This application searches only Word files in SharePoint whereas it should have the capability to find all other types especially PDF, XLS, PPT, etc. This is not much work and only a couple of lines need to be included in the code. I promise I will do it in next release.
3. Application saves the Word file in the folder where the executable resides. Application should have allowed the user to select a location on the hard disk. This will be included in next release.
Here is what you need to do to run the executable:
1. Edit the db.txt file which is located in the same folder where the EXE resides. This file contains the db connection string. Modify the connection string in this file.
Data Source=sqlserver;Initial Catalog=myPortal1_SITE;User Id=sa;Password=abcd;
"Data source" is your sql server name. "Initial Catalog" contains the name of your portal server site database, for example, if your portal name is "myPortal" then most probably, your database name will be "myPortal1_SITE". Remember, there are three main databases associated with your portal:
a. myPortal1_PROF
b. myPortal1_SERV
c. myPortal1_SITE
We are concerned about the third one only, myPortal1_SITE. You also need to provide User Id and Password (if any) for your database. Modify the connection string and save the db.txt file.
The zip file attached contains only two files: SPDocPuller.exe and db.txt. I could have created an installer but that would have taken a lot of space unnecessarily. I assume you will be running this tool on the SharePoint box. The executable references following files, therefore these should be available on your machine:
1. Microsoft.Office.Core
2. Microsoft.SharePoint.Portal
3. Microsoft.VisualBasic
4. Scripting
Clicking the "Pull Document" button will save the file in the same folder from where you will run the executable file.
I will be looking forward to your comments and suggestions.
Thanks,

-SSA

Friday, June 16, 2006

Manipulating SharePoint list's metadata from inside InfoPath

Yes, this is possible. You can manipulate SharePoint's list metadata from inside InfoPath. You must have InfoPath toolkit installed. See following post for reference:

/ssa/archive/2006/06/02/8029.aspx

You can get/set values in SharePoint list. Recently, I was working on a workflow solution that involved both SharePoint and InfoPath. Till now, all the workflow solutions that I developed used SharePoint as the main tool and workflow was built into SharePoint but this time, the requirement was a bit different. We wanted to build the workflow solution directly in InfoPath form and part of this requirement was to set a property's value in document library directly from InfoPath form. "Approval Status" field in form library can contain one of the following values:

1. Approved
2. Rejected
3. Pending

Depending on your form's requirements, you can set the value of "Approval Status" field directly from InfoPath. Here is the code:

SPWeb mySite = new SPSite("http://yourportal/sites/HR/HRForms/Forms/mod-view.aspx").OpenWeb();
SPFolder myFolder = mySite.GetFolder("HRForms");
SPFileCollection myFiles = myFolder.Files;
try
{
foreach (SPFile fyle in myFiles)
{
fyle.Item.ModerationInformation.Status = Microsoft.SharePoint.SPModerationStatusType.Pending;
fyle.Item.Update();
}
}


Code Explanation:

Open your site using OpenWeb() method. In the code above, first line opens a site called "HR". "HR" site has a folder named "HRForms" and this folder contains all forms. In the for-each loop, we are setting each file's status to "Pending". It is important to call the Update() method in order to apply the changes.

Add a reference to Microsoft.SharePoint (Microsoft.SharePoint.dll) and add following namespaces in the code:

using Microsoft.SharePoint;
using System.Collections;

-SSA

Saturday, June 3, 2006

Checking User Permissions in SharePoint

Following is the code to check the rights of the users. Following code will show only those sites for which user has read privileges.

    Function CheckRights(ByVal sitePath As String, Byval strUser as string) As Boolean
    //sitePath contains the site that you retrieved in the code shown above
    // strUser contains the user for whom you want to check the rights and retrieve sites
        Try
            Dim blnStatus As Boolean = False
            If Not sitePath Is Nothing Or Not sitePath = "" Then
                Dim rtn As String() = parseURL(sitePath)
                    Dim siteCollection As SPSite
                    siteCollection = New SPSite(sitePath)
                    Dim site As SPWeb = siteCollection.OpenWeb(rtn(0))
                    Dim allUsers As SPUserCollection = site.Users
                    Dim user As SPUser
                    For Each user In allUsers
                        If user.LoginName.ToUpper = strUser Then
                            Dim allGroups As SPRoleCollection = user.Roles
                            Dim group As SPRole
                            For Each group In allGroups
                                Dim right As Integer
                                right = group.PermissionMask And SPRights.ViewListItems
                                If right = SPRights.ViewListItems Then
                                    blnStatus = True
                                    Return blnStatus
                                    Exit Function
                                End If
                            Next
                        End If
                    Next

                Return blnStatus
            End If
        Catch ex As Exception
            //Exception handling code here!
        End Try
    End Function

The function shown above (CheckRights) uses another function called as parseURL(). Following is the code for the parseURL() function:

Public Function parseURL(ByVal _fullPath As String) As String()
_fullPath = System.Web.HttpUtility.UrlDecode(_fullPath)
Dim site As SPSite = New SPSite(_fullPath)
Dim web As SPWeb = site.RootWeb
Dim webPath As String = _fullPath.Replace(web.Url & "/", "")
Dim rtnObject As String() = {"", "", ""}
Dim w As SPWeb
Dim folderPath As String = ""
Dim docLibPath As String = ""
Dim i, j As Integer
While CType(w, Object) Is Nothing
Try
w = site.OpenWeb(webPath)
Dim s As String = w.ID.ToString
'//above will raise the exception too
Catch ex As Exception
w = Nothing
i = webPath.LastIndexOf("/")
If i = -1 Then
Exit While '//means the end of subsite lookup
End If
j = webPath.Length
webPath = webPath.Remove(i, j - i)
End Try
End While
'//if there was some sub-site object then
If Not CType(w, Object) Is Nothing Then
folderPath = _fullPath.Substring(_fullPath.IndexOf(webPath) + webPath.Length + 1)
If folderPath.IndexOf("/") > 0 Then
docLibPath = folderPath.Substring(0, folderPath.IndexOf("/"))
folderPath = folderPath.Remove(0, folderPath.IndexOf("/") + 1)
End If
If docLibPath = webPath Then webPath = "" '_fullPath
rtnObject(0) = webPath
rtnObject(1) = docLibPath
rtnObject(2) = folderPath
Else '//there was no sub-site obect was found
folderPath = _fullPath.Substring(_fullPath.IndexOf(webPath))
If folderPath.IndexOf("/") > 0 Then
docLibPath = folderPath.Substring(0, folderPath.IndexOf("/"))
folderPath = folderPath.Remove(0, folderPath.IndexOf("/") + 1)
End If
If docLibPath = webPath Then webPath = "" '_fullPath
rtnObject(0) = webPath
rtnObject(1) = docLibPath
rtnObject(2) = folderPath
End If
Return rtnObject
End Function

Note: I know this will seem a bit complicated to you if you are a beginner but believe me it is not that complicated. It's pretty straight forward.
1. You retrieve sites from the portal.
2. You plug in CheckRights() function that checks whether the site has a user you passed in as a parameter and whether that user has reader rights.
3. parseURL() function is used by the CheckRights() function.
Just copy and paste the above functions, you may need to do minor tweaking and it should work fine.

-SSA