何小碩's profileGet More... ExperiencePhotosBlogListsMore Tools Help

Blog


    February 22

    MOSS 2007 - Conditionally start a workflow with an eventhandler in SharePoint 2007

     

    One of the major improvements in both WSS 3.0 and Microsoft Office SharePoint Server 2007 is the enhanced event handler framework. In SPS 2003 only document libraries supported event handlers, in WSS 3.0 and MOSS2007 there are also event handlers at site level and for all types of lists (both document libraries and lists). Another change is the support for both synchronous and assynchronous events - this means that an event also fires before an action occurs. There is for example an ItemAdded event as well as an ItemAdding event - the "Ed" event occurs after committing to the database and "Ing" event before committing changes to the database.

    The base class for all eventhandling in SharePoint 2007 is the SPEventReceiverBase (Microsoft.SharePoint) class. The different classes you will need to inherit from to eventhandler are SPListEvenReceiver - for a list , SPWebEventReceiver - for site or site collection (deleted, deleting, moved, moving) and SPItemEventReceiver for individual items. In the next sample, I will create an event handler for a listitem which will start up a new workflow. To do this you will need to complete the next steps:

    • Create a new class library project in Visual Studio 2005
    • Add a reference to the Microsoft.SharePoint.dll to get access to the SPItemEventReceiver class
    • Add a custom class inheriting from the SPItemEventReceiver class
    • To get the event handler to work you will need to override some methods - in this case the ItemAdded and ItemUpdated methods
    public override void ItemAdded(SPItemEventProperties properties)
    {
    ConditionalStartWorkflow(properties);
    }

    public override void ItemUpdated(SPItemEventProperties properties)
    {
    ConditionalStartWorkflow(properties);
    }
    private void ConditionalStartWorkflow(SPItemEventProperties properties)
    {
    try
    {
    SPListItem item = properties.ListItem;
    string sDraft = item["Draft"].ToString();
    bool bDraft = bool.Parse(item["Draft"].ToString());

    //Start workflow only when it is not a draft version
    if (!bDraft)
    {
    //Only start workflow - when there is no workflow active yet
    if (item.Workflows.Count == 0)
    {
    SPWorkflowManager wfmgr = item.Web.Site.WorkflowManager;
    SPWorkflowAssociationCollection wfassoccol = _
    item.ParentList.WorkflowAssociations;

    foreach (SPWorkflowAssociation wfassoc in wfassoccol)
    {
    //Find the GUID for the workflow association
    if (String.Compare(wfassoc.BaseId.ToString("B"), _
    "{c6964bff-bf8d-41ac-ad5e-b61ec111731c}", true) == 0)
    {
    wfmgr.StartWorkflow(item, wfassoc, "", true);
    }
    }
    }

    }
    }
    catch (Exception ex)
    {
    System.Diagnostics.EventLog.WriteEntry("StartHandler", ex.ToString());
    }
    }
    • Add a strong key to the library and build it. Then copy the dll you built to the Global Assembly Cache (GAC).

    In SharePoint 2003 you could use the user interface to register an event handler, this is not possible anymore. There are however two other options:

    • Use the WSS features framework - this framework provides you with a way to package, deploy and activate your own customisations on top of SharePoint.
    • Do it through code (No UI as in WSS 2.0)

    How to: Handle an Event using .NET in MOSS 2007

     

    This example shows how to add a simple event handler that prevents items from being deleted from a list. Two procedures are involved in this task:

    * Creating an event handler in Microsoft Visual Studio
    * Adding the event handler as a Feature in Windows SharePoint Services

    To create the event handler in Visual Studio

    1. Create a new project in Visual Studio by clicking File, pointing to New, and then clicking Project.
    2. In the New Project dialog box, select Visual C# in the Project types box, select Class Library in the Templates box, type DeletingEventHandler in the Name box, and then click OK.
    3. In Solution Explorer, select DeletingEventHandler, and click Add Reference on the Project menu.
    4. In the Add Reference dialog box, select Microsoft.SharePoint on the .NET tab and then click OK.
    5. In the Code Editor, import the Microsoft.SharePoint namespace as follows.

    using Microsoft.SharePoint;


    6. Change the name of the class to DeletingAction and make it inherit from the SPItemEventReceiver class, as follows.

    public class DeletingAction : SPItemEventReceiver


    7. Add the following code within the class to override the ItemDeleting method in C#

    1. public override void ItemDeleting(SPItemEventProperties properties) 
    2.     properties.Cancel = true
    3.     properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported."

    public override void ItemDeleting(SPItemEventProperties properties)
    {
    properties.Cancel = true;
    properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported.";
    }


    8. In Solution Explorer, right-click the DeletingEventHandler node, and then click Properties.
    9. In the Properties dialog box, click the Signing tab, select Sign the asembly, select Choose a strong name key file, and then click <New…>.
    10. In the Create Strong Name Key dialog box, type DeletingEventHandler.snk in the Key file name box, optionally specify a password for the key, and then click OK.
    11. To build the project, click Build Solution on the Build menu, or press CTRL+SHIFT+B.
    12. Find the \DeletingEventHandler\bin\Debug folder in the Visual Studio Projects folder, and drag the DeletingEventHandler.dll file to Local_Drive:\WINDOWS\assembly to place the DLL in the global assembly cache.

    To add the event handler as a Windows SharePoint Services Feature

    1. Create a folder in Local_Drive:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/FEATURES called DeletingEventHandler.
    2. Create a Feature.xml Files file in this folder like the following that identifies the Feature and its element manifest file and sets the Feature scope to Web site.
    Xml

    1. <Feature Scope="Web"  
    2.    Title="Deleting Event Handler"  
    3.    Id="GUID"  
    4.    xmlns="http://schemas.microsoft.com/sharepoint/"
    5.    <ElementManifests> 
    6.       <ElementManifest Location="Elements.xml"/> 
    7.    </ElementManifests> 
    8. </Feature> 

    <Feature Scope="Web"
    Title="Deleting Event Handler"
    Id="GUID"
    xmlns="http://schemas.microsoft.com/sharepoint/">
    <ElementManifests>
    <ElementManifest Location="Elements.xml"/>
    </ElementManifests>
    </Feature>


    3. To replace the GUID placeholder in the previous Id attribute, generate a GUID by running guidgen.exe located in Local_Drive:\Program Files\Microsoft Visual Studio 8.
    4. Create an Elements.xml file in the DeletingEventHandler folder that identifies the assembly, class, and method to implement as the event handler. This example applies the event handler to all announcements lists of a site, as specified by the ListTemplateId attribute. For the IDs of other default Windows SharePoint Services list template types, see the Type attribute description of the ListTemplate element.
    Xml
    1. <Elements xmlns="http://schemas.microsoft.com/sharepoint/"
    2.    <Receivers ListTemplateId="104"
    3.       <Receiver> 
    4.          <Name>DeletingEventHandler</Name> 
    5.          <Type>ItemDeleting</Type> 
    6.          <SequenceNumber>10000</SequenceNumber> 
    7.          <Assembly>DeletingEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a26b5449ac4a4cf3</Assembly> 
    8.          <Class>DeletingEventHandler.DeletingAction</Class> 
    9.          <Data></Data> 
    10.          <Filter></Filter> 
    11.       </Receiver> 
    12.    </Receivers> 
    13. </Elements> 

    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <Receivers ListTemplateId="104">
    <Receiver>
    <Name>DeletingEventHandler</Name>
    <Type>ItemDeleting</Type>
    <SequenceNumber>10000</SequenceNumber>
    <Assembly>DeletingEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a26b5449ac4a4cf3</Assembly>
    <Class>DeletingEventHandler.DeletingAction</Class>
    <Data></Data>
    <Filter></Filter>
    </Receiver>
    </Receivers>
    </Elements>


    5. To get the Public Key Token of the assembly, in Windows Explorer find the DeletingEventHandler.dll file in the Local_Drive:\WINDOWS\assembly, right-click the file, click Properties, and on the General tab of the Properties dialog box, select and copy the token.
    6. At a command prompt, navigate to \Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN on the local drive, and type each of the following commands to install the Feature in the deployment, activate the Feature on a specified subsite, and reset Microsoft Internet Information Services (IIS) so that the changes take effect:

    stsadm -o installfeature -filename DeletingEventHandler\Feature.xml

    stsadm -o activatefeature -filename DeletingEventHandler\Feature.xml -url http://Server/Site/Subsite

    iisreset


    7. Try to delete an item in an announcements list on the specified Web site to see the effects of the event handler Feature.

    comes form:http://www.sharepoint-tips.com/2006/06/event-handlers-in-moss-sharepoint-2007.html

    How to schedule MOSS 2007 backup

     

    1. Set path of stsadm.exe
    My computer -> Right click ->properties-> Advanced ->Environment Variables->system variable ->select “path” -> click edit
    Put ; after existing path and give path of stsadm.exe Generally it is
    C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\
    Click ok, type stsadm in command prompt you should be able to access it without having to go through the trouble of navigating to the SharePoint BIN folder.
    We will use following command to perform backup
    stsadm.exe -o backup -directory "\\Backup Folder " -backupmethod full -item "Farm" –overwrite
    2. If your database server is different from MOSS server then you need to
    >> share backup folder in MOSS Server and give folder write access to the user that runs backup command and that has permission to run other sharepoint services.
    >> Verify that the MSSQLServer service on database server is started under a domain account that has write access to both the Server share and its underlying partition. Click here to configure domain account in Sql server services.
    3. Assuming your backup folder is c:\backup and your MOSS server name is “server” then create a batch file and type following
    ------------------------------------------------------------------------------------
    cd\
    cd C:\backup
    rd %date:~10,4%-%date:~4,2%-%date:~7,2% /s/q
    md %date:~10,4%-%date:~4,2%-%date:~7,2%
    stsadm.exe -o backup -directory "\\server\Backup\%date:~10,4%-%date:~4,2%-%date:~7,2%" -backupmethod full -item "Farm" –overwrite
    ------------------------------------------------------------------------------------
    It will create date wise folder (yyyy-MM-dd) and put backup in it.
    change folder path in batch file as per your requirement.
    4. Run batch file to confirm everything is okay. If you get following error:
    Error: Object SharePoint_Config failed in event OnBackup. For more information, see the error log located in the backup directory. SqlException: Cannot open backup device '\\server\backup\spbr0001\0000001.bak'. Operating system error 5(error not found).
    Then configure sql server services to domain account. Click here to see it.
    5. For Scheduling:
    >> open control panel -> Scheduled Tasks ->Add scheduled Task
    >> Select batch file and schedule type – daily
    >> Give time
    >> Enter user info
    >> Click finish

    Related Posts :

    MOSS 2007

    comes from:http://jopx.blogspot.com/2006/10/moss-2007-conditionally-start-workflow.html

    Convert Team site / blank site to a publishing site in MOSS 2007

     

    Publishing Site vs. Team Site
    When you first start using SharePoint you most likely will first have a SharePoint Team Site. This is a template that enables various Features of SharePoint that are intended for team collaboration. If you are like me, this is not an appropriate setup for a public facing internet site. For this, you should be using the Publishing Site template. Here is some verbiage from Microsoft regarding some differences:
    Publishing Site
    Select this site template if you want to create a blank Web site and quickly publish Web pages. This template includes document and image libraries for storing Web publishing assets. Contributors can work on draft versions of pages and publish them to make them visible to readers. The site includes document and image libraries for storing Web publishing assets.
    Team Site
    Select this site template when you want to create a site that teams can use to create, organize, and share information. The template includes a document library, an announcements list, a calendar, a contacts list, and a links list.

    What happen if you select Team site by mistake. You will get following major effect
    a. Some options like Create Page, edit page, manage content and structure… etc are missing.
    b. The default page doesn’t belong to page library.
    c. ‘Advance Search’ option is not there.

    Don’t worry, here is the solution

    solution (a.)
    1. Go to “Site Settings” ->”Site Collection Features
    2. Activate “Office SharePoint Server Publishing” and “Office SharePoint Server Publishing Infrastructure” (IF Available) services.
    Now you will see all options in Site Action menu.

    solution (b.)
    3. Go to top level site: Site Action -> Create New Page
    4. Select “Welcome page with summary links” option
    5. Give title, description and url and click on create button.
    6. Now Go to “Site Settings” ->”Site Welcome Page
    7. Browse and select “welcome page”(which you created) from “pages” library.

    solution (c.)
    8. Goto top level site and select “site settings” and select “Sites and workspaces” in site administration group and click on create.
    9. Give title and url. Select “Search Center” template from “Enterprise” tab and create it.

    10. Now you will be redirected to search page, copy url (say URL-A).
    11. In Top level, go to Site settings->Search settings (in site collection administration group)
    12. Select the “Use Custom scope ….” Option and paste the url (URL-A) and remove page name from url.
    For Example if your url is “http://myserver/Search/Advanced.aspx” then you have to paste
    http://myserver/Search/” only.
    Now Go to Home page and Enjoy it.

    comes from:http://urenjoy.blogspot.com/2008/12/convert-team-site-blank-site-to.html

    February 07

    How to fix SharePoint 2007 and WSS Event Errors 6481 and 6389

    10/30/2007

    I ran in to this error on a default Single Server install for my customer, a small real estate agency in San Diego. Normally I would have caught this DCOM authentication error in the system log, but instead focused on the symptoms of the problem in the application log, and wasted 60 minutes of time doing so (Bad John).  So in order to help save some time in the future, I'll post the quick fix below.

    1. Open Start Menu > Administrative Tools > Component Services Tool

    2. Drill Down and Expand DCOM Config

    image

    3. Navigate to the OSearch, Right Click Properties

    image

    4. Select the Security Tab

    image

    5. Add the Network Service Account to Local and Activation Permissions

    image

    6. Add the Network Service Account to Access Permissions

    image

    7. Restart the Office SharePoint Server Search Service from Start Menu > Administrative Tools > Services Tool

    image

    Related Errors:
    Application Event Log: Event ID 6398: Windows SharePoint Services Error

    Event Type:    Error
    Event Source:    Windows SharePoint Services 3
    Event Category:    Timer
    Event ID:    6398
    Date:        10/30/2007
    Time:        7:48:39 PM
    User:        N/A
    Computer:    PWEB
    Description:
    The Execute method of job definition Microsoft.Office.Server.Search.Administration.IndexingScheduleJobDefinition (ID 68e33a4b-562e-4840-ab86-2c8d8febdd75) threw an exception. More information is included below.

    Retrieving the COM class factory for component with CLSID {3D42CCB1-4665-4620-92A3-478F47389230} failed due to the following error: 80070005.

    Application Event Log: Event ID 6481: Office SharePoint Server Error

    Event Type:    Error
    Event Source:    Office SharePoint Server
    Event Category:    Office Server Shared Services
    Event ID:    6481
    Date:        10/30/2007
    Time:        7:48:10 PM
    User:        N/A
    Computer:    PWEB
    Description:
    Application Server job failed for service instance Microsoft.Office.Server.Search.Administration.SearchServiceInstance (58cf8651-9768-4774-8dc6-d4b3483a4bd8).

    Reason: Retrieving the COM class factory for component with CLSID {3D42CCB1-4665-4620-92A3-478F47389230} failed due to the following error: 80070005.

    Techinal Support Details:
    System.UnauthorizedAccessException: Retrieving the COM class factory for component with CLSID {3D42CCB1-4665-4620-92A3-478F47389230} failed due to the following error: 80070005.
       at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(RuntimeType objectType)
       at System.Runtime.Remoting.RemotingServices.AllocateUninitializedObject(Type objectType)
       at System.Runtime.Remoting.Activation.ActivationServices.CreateInstance(Type serverType)
       at System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(Type serverType, Object[] props, Boolean bNewObj)
       at Microsoft.Office.Server.Search.Administration.Gatherer.get_AdminObject()
       at Microsoft.Office.Server.Search.Administration.Gatherer.ProvisionGlobalProperties()
       at Microsoft.Office.Server.Search.Administration.SearchServiceInstance.Synchronize()
       at Microsoft.Office.Server.Administration.ApplicationServerJob.ProvisionLocalSharedServiceInstances(Boolean isAdministrationServiceJob)

    System Event Log: Event ID 10016: DCOM Error

    Event Type:    Error
    Event Source:    DCOM
    Event Category:    None
    Event ID:    10016
    Date:        10/30/2007
    Time:        7:55:39 PM
    User:        NT AUTHORITY\NETWORK SERVICE
    Computer:    PWEB
    Description:
    The application-specific permission settings do not grant Local Activation permission for the COM Server application with CLSID
    {3D42CCB1-4665-4620-92A3-478F47389230}
    to the user NT AUTHORITY\NETWORK SERVICE SID (S-1-5-20).  This security permission can be modified using the Component Services administrative tool.

    Posted at 11:56 AM by John Gilham |