65.9K
CodeProject is changing. Read more.
Home

Working with two or more SharePoint Lists

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.08/5 (6 votes)

Oct 24, 2008

CPOL

2 min read

viewsIcon

28755

downloadIcon

238

Updating different List values from an event occuring in a List.

Introduction

Normally when we want to update different number of list values during an event from another list where a value has been updated, during design time, it is not possible.

Through SharePoint Object Programming, we can custom program the things we need, what things to update after an event has been triggered, and all.

Background

For custom programming in VS.NET, we need to extend the VS Extension for SharePoint, then the following steps to be done:

  1. Create a New Class Library Project and name it.
  2. Refer the Microsoft.Sharepoint.dll in this project.
  3. Inherit the SPItemEventReceiver class to receive the events from the specified list events.
  4. Override the different events inside the inherited class to do custom update.
  5. Create a StrongNameKey .snk file for the authentication in the properties of the project.
  6. Create a new Console project to specify the SPSite where the lists are located.
  7. Specify the SPEventReceiverType to the SPList of the above site.
  8. First Build the Class Library Project and copy the .dll file to the C:\Windows\Assembly folder.
  9. Set the Console project as the StartUp Project.
  10. Run the Console project by pressing F5.
  11. Reset IIS by executing 'iisreset' in the Run command to make the changes reflected in the actual SPSite.
  12. Go to the SharePoint Server WSS/MOSS to see the update.

Using the Code

Following is the Class Library Project where you have to have these:

  • SPSite - SharePoint Site or Web Site
  • SPList - SharePoint List or Table View
  • SPListItem - SharePoint ListItem or field of the List
  • SPQuery - SharePoint Query to embed the CAML Queries
  • CAML - Collaborative Markup Language
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Windows.Forms;

namespace twolists
{
public class Class1:SPItemEventReceiver
{
    public override void ItemAdded(SPItemEventProperties properties)
    {
        upd_bks_status(properties);
    }

    void upd_bks_status(SPItemEventProperties properties)
    {
        SPListItem acListItem = properties.ListItem;
        string lkbkid=acListItem["lkBooksAvail"].ToString();

        lkbkid= lkbkid.Substring(lkbkid.IndexOf('#') + 1);
        SPSite st = new SPSite("http://win2003/physlib");
        SPWeb wb=st.OpenWeb();
        SPList bklst=wb.Lists["Books"];
        SPQuery qry = new SPQuery();

        qry.Query = "<Where><Eq><FieldRef " + 
            "Name='Title'/><Value Type='Text'>" + 
            lkbkid + "</Value></Eq></Where>";
        SPListItemCollection itlst = bklst.GetItems(qry);
        SPListItem updList = itlst[0];
        updList["Status"] = "Taken";
        updList.Update();
        acListItem["Issue Status"] = lkbkid+" Issued Successfully";
        acListItem.Update();
    }
}
}

Following is the Console Application where you do these:

  1. Build the Class Library Project and get the .dll and copy it to the Windows assembly folder.
  2. Reflect that assembly file name in this code.
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
namespace twolistsConsole
{
class Program
{
    static void Main(string[] args)
    {
        SPSite st = new SPSite(@"http://win2003/physlib");
        SPWeb wb = st.OpenWeb();
        SPList ls = wb.Lists["Transactions"];

        //SPList rs = wb.Lists["Return Books"];
        string asmnam = "twolists,Version=1.0.0.0," + 
               "Culture=neutral,PublicKeyToken=eda8b4ba519c54dd";
        string clsnam = "twolists.Class1";
        ls.EventReceivers.Add(SPEventReceiverType.ItemAdded, asmnam, clsnam);
        //rs.EventReceivers.Add(SPEventReceiverType.ItemAdded, asmnam, clsnam);
    }
}
}
  1. Run the Console project and reset IIS.
  2. Go to the SharePoint Site to see the update.

Happy programming..