Wednesday, December 29, 2010

Workflow in Moss 2007-Sending email tips

This is what i learned today about Work flow in Moss 2007.In our project we have a requirement of sending email after a particular step in the work flow.

I tried writing down simple texts and look up in the email body.

To my surprise when the work flow executes and email is send to the assigned person,the mail goes to the junk folder of the mailbox.

But when i use HTML format in the mail body,the mail goes to the desired person mailbox instead of junk folder.So the conclusion is always use HTML format in the body of the email while working with Custom work flow in sharepoint designer 2007.

Saturday, December 25, 2010

How to set Item Level Permission for SharePoint 2007 (MOSS/WSS) List/Document Library Programmatically


Here is a piece of code (a function) to set Item Level Permission. You can use it as a Web Method in a custom Web Service. This method can be used from Applications outside of SharePoint, provided the user using this application has sufficient privilege to update lists/libraries etc.

    public string ItemPermission(string SitePath)
    {
        string ReturnVal = "";
        try
        {
            SPSite WebApp = new SPSite(SitePath);
            SPWeb Site = WebApp.OpenWeb();
            SPList list = Site.Lists["TestDocLib"];
            SPListItem item = list.Items[0];
            SPRoleDefinition RoleDefinition = Site.RoleDefinitions.GetByType(SPRoleType.Contributor);
            SPRoleAssignment RoleAssignment = new SPRoleAssignment("<domain>\\<user>", "email", "name", "notes");
            RoleAssignment.RoleDefinitionBindings.Add(RoleDefinition);
            if(!item.HasUniqueRoleAssignments)
            {
                item.BreakRoleInheritance(true);               
            }
            item.RoleAssignments.Add(RoleAssignment);
            item.Update();
        }
        catch (Exception ex)
        {
            ReturnVal += "Permission not set, reason: " + ex.Message;
        }
        return ReturnVal;
    }




Requirement:

I have a list and have made settings wherein the user can edit only the items created by them and read others data. Now if a person leaves the company all the data created by he/she will become read only to others. There is no apparent OOB way to give permission to any other user to those items at one go. But we can use custom coding and special ability of SharePoint 2007 to set Item level permission for this requirement.

I have created a Custom Web Service to do the trick (From here you will get information about how to implement this web service in SharePoint). And there is a console application to pass the parameters to the Web Service’s web method. You can replace this console app with Windows/Web Form, Web Part etc. Or you can create a custom workflow which will get activated when any user is removed and will call the web service.

Here is the code for the web service:

===================================================  
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Microsoft.SharePoint;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public string ItemPermission(string SitePath, string LibName, string OldUser, string NewUser,string email, string name)
    {

        string ReturnVal = "";

        try
        {
            SPSite WebApp = new SPSite(SitePath);
            SPWeb Site = WebApp.OpenWeb();
            SPList list = Site.Lists[LibName];
            SPQuery newSPQuery = new SPQuery();
            newSPQuery.Query = "<Where><Eq><FieldRef Name=\"Author\"/><Value Type=\"User\">" + OldUser + "</Value></Eq></Where>";
            SPListItemCollection listItemCol = list.GetItems(newSPQuery);
            if (listItemCol.Count > 0)
            {
                foreach (SPListItem item in listItemCol)
                {
                    SPRoleDefinition RoleDefinition = Site.RoleDefinitions.GetByType(SPRoleType.Contributor);
                    SPRoleAssignment RoleAssignment = new SPRoleAssignment(NewUser, email, name, "notes");
                    RoleAssignment.RoleDefinitionBindings.Add(RoleDefinition);
                    if (!item.HasUniqueRoleAssignments)
                    {
                        item.BreakRoleInheritance(true);
                    }
                    item.RoleAssignments.Add(RoleAssignment);
                    item.Update();
                }
            }
        }
        catch (Exception ex)
        {
            ReturnVal += "Permission not set, reason: " + ex.Message;
        }
        return ReturnVal;
    }
   
}
===================================================

Here is the code for console application:

Replace the following things:

<sitepath> with the Full URL of the site
<libname> with the list/library name
<domain> with the domain name
<olduser> with the userid who left the company
<newuser> with the userid to whom you want to give permission
<email of new user> self explaning
<name of new user> self explaning

If "<domain>\\<olduser>" does not work try to use the old user’s full name such as “John Smith”.

=====================================================

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        //localhost.Service newService;
        static void Main(string[] args)
        {
            localhost.Service newService = new localhost.Service();
            newService.UseDefaultCredentials = true; //I am assuming an administrator/power user is running this app or use a specific credential here
            string output = newService.ItemPermission("<sitepath>""<libname>""<domain>\\<olduser>""<domain>\\<newuser>""<email of new user>""<name of new user>");
            Console.WriteLine(output);
            Console.ReadLine();
        }
    }
}



Source:msdn

Saturday, December 4, 2010

Most Common Terms in SharePoint


Farm – collection of servers that act together to provide a set of Web applications. The farm is most easily defined by the configuration database which defines the server member and roles of the farm as well as database servers used for storage.

Web application – In a SharePoint deployment contains IIS Web Sites previously known as IIS Virtual Servers that have been extended with SharePoint Server and have been set up to use the .NET assemblies to provide the SharePoint Application. There are content Web applications, and administration Web applications for both the central admin and the SSP admin. Web applications are known for their ability to isolate content in separate memory space with application pools which contain worker processes. In a collaboration service the Web application provides the container for the site collections in the farm.

Site Collection – A site collection is the most scalable unit in a SharePoint deployment and is a container for sites. The special properties of the site collection are the ability to have a quota, contain a global navigation, masterpage, and various galleries provided across the sites below it, and the ability to contain ownership. Considered the most scalable object in a deployment and the easiest to manage for its ability to be backed up and moved full fidelity across databases.

Site – a container of multiple lists which can inherit security, and leverage the galleries of the site collection above it. Primarily usage in a collaboration environment is for delegation of projects or to divide up content for easy navigation.

Portal – A special site template designed to host many sites below it. Common properties include special page galleries for Internet, or site directory on an Intranet portal. Another term might be hub based on navigation and a common place to go where you don’t know where to look for something. It’s the top of the breadcrumb navigation.

Thursday, December 2, 2010

Splitting of content database

FYI The recommended size for a Content Database in SharePoint 2007 is: 100 GB


case study:
The Size of content Database of intranet site is 450 GB, with this content database size XYZ is facing issues in managing such a huge database.So in this case we have to split the content database.


One solution may be like this :

If there is only one Site Collection in the Content DB which consumes 450 GB then it's not possible to split the content Database but we have to tweak with subsites.

If  SiteCollection count >1 then we can move the Data of some selected site collections to another Content DB.

Add new content database (demo_content_2) from Central Administration àApplication Management à Content Databases to the existing Webapplication as shown below:
http://blogs.technet.com/photos/corybu/images/1315131/original.aspx 
From the above screenshot the delta for demo_content_1 is (500 – 3 = 497) and the delta for demo_content_2 is (500 – 0 = 500).  The larger the delta is determines where your site goes.  So the next site collection created will go into demo_content_2.

To perform the move of SiteCollections use the following 3 commands:

stsadm -o backup -url http://demo/sites/test2 -filename e:\test2.bak
stsadm -o deletesite -url http://demo/sites/test2
stsadm -o restore -url http://demo/sites/test2 -filename e:\test2.bak

If we refresh the content database page we can see that sitecollection has been moved to demo_content_2.
 http://blogs.technet.com/photos/corybu/images/1315132/original.aspx

To perform the move of subsites use the following 3 Commands:

stsadm -o export -url http://<path_to_site> -filename c:\<exportedSite>.cmp -overwrite -nofilecompression -includeusersecurity -cabsize 1024
Delete the subsite via SharePoint UI or stsadm command
stsadm -o import –url http://<path_to_site> -filename c:\<location_of_your_exorted_package>.cmp -nofilecompression -haltonfatalerror –includeusersecurity”
Now the sub-site would be created in new content database demo_content_2.
Note: A subsite has to be restored to a subsite only but not to a sitecollection.



First things first

There are 3 things which are most important for a sharepoint developer and those are

1)Site Columns
2)Content types
3)Workflow

With increased demand of sharepoint everybody should have a basic idea about sharepoint.
SPS 2003 was the first portal server,then comes MOSS 2007 with office integration and now SP 2010 most improved product of its type.