Click here to Skip to main content
15,867,785 members
Articles / Programming Languages / C#

Working with two or more SharePoint Lists

Rate me:
Please Sign up or sign in to vote.
2.08/5 (6 votes)
23 Oct 2008CPOL2 min read 28.5K   21   4
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
C#
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.
C#
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..

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralWorking with SharePoint list Pin
labshasan22-May-11 0:40
labshasan22-May-11 0:40 
GeneralWorking with two or more Sharepoint Lists Pin
Riazul Islam Mazumder5-Aug-09 1:23
Riazul Islam Mazumder5-Aug-09 1:23 
Generaldebug Pin
joekey9-Apr-09 11:17
joekey9-Apr-09 11:17 
GeneralSubsites Pin
Anoosh Rahman4-Nov-08 1:11
Anoosh Rahman4-Nov-08 1:11 
Hi It's working fine in the Root site , but NOT in subsites ..

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.