SharePoint Development Bookmark and Share   
 index > SharePoint - Development and Programming > Problem with FeatureReceiver
 

Problem with FeatureReceiver

Hi all,

I have 2 customized lists.
1) "Bank holiday" list.
2) "Team member" list.

Business case to be implemented:

If in current calendar year(2009) has weekends (Bank or public holiday list) then in "Team member list" each member leave to be increased.

Problem:

I have implemented SPFeatureReceiver and created a function for checking if bank holiday is falls on weekend then each team member holiday to be increased. But teamMember.Update(); function is not updating.

-------------------
foreach (SPListItem item in items)
{
weekend = isWeekend(Convert.ToDateTime(item["Day"]));
item["Day"].ToString();
if (weekend)
{
SPListItemCollection lc = teamList.GetItems(queryTeam);
foreach(SPListItem item2 in lc)
{
item2["TotalNumberOfLeaves"] = Convert.ToInt16(item2["TotalNumberOfLeaves"]) + 1;

teamList.Update(); // NOT UPDATING THE LIST!!
}
}
}


-------------------

Could someone help to fix me issue?

This is my latest code.

Thank you.

Cheers,
Aroh

usingSystem;
usingSystem.Runtime.InteropServices;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Text.RegularExpressions;
usingMicrosoft.SharePoint;
usingMicrosoft.SharePoint.WebControls;
usingMicrosoft.SharePoint.Utilities;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
usingSystem.IO;
namespaceCalendarFeatureHandler
{
publicclassCalendarAction:SPFeatureReceiver
{
publicoverridevoidFeatureActivated(SPFeatureReceiverPropertiesproperties)
{
//thrownewNotImplementedException();
SPWebsite=properties.Feature.ParentasSPWeb;
//Gettinglistreferences"Bankholidays"and"Teammembers"
SPListbankList=site.Lists["Bankholidays"];
SPListteamList=site.Lists["Teammembers"];
//flagtocheckthebankholidayfallsovertheweekend
boolweekend;
SPListItemCollectionitems=null;
//ForBankholidaysandTeamlistlists
SPQueryqueryBank=newSPQuery();
SPQueryqueryTeam=newSPQuery();
//Queriesfor"Bankholiday"and"Teamlist"
queryBank.Query="<OrderBy><FieldRefName='ID'/><FieldRefName='Holiday_x0020_description'/><FieldRefName='Day'/></OrderBy>";
queryTeam.Query="<OrderBy><FieldRefName='ID'/></OrderBy>";
items=bankList.GetItems(queryBank);
foreach(SPListItemiteminitems)
{
weekend=isWeekend(Convert.ToDateTime(item["Day"]));
item["Day"].ToString();
if(weekend)
{
SPListItemCollectionlc=teamList.GetItems(queryTeam);
foreach(SPListItemitem2inlc)
{
item2["TotalNumberOfLeaves"]=Convert.ToInt16(item2["TotalNumberOfLeaves"])+1;
teamList.Update();
}
}
}
teamList.Update();//NOTUPDATINGTHELIST
bankList.Update();
}
///<summary>
///Computingnoofweekendforacurrentcalendaryear
///</summary>
///<paramname="weekend"></param>
///<returns></returns>
publicstaticboolisWeekend(DateTimeweekend)
{
if(weekend.DayOfWeek.ToString().Trim().Equals("Saturday")||weekend.DayOfWeek.ToString().Trim().Equals("Sunday"))
returntrue;
else
returnfalse;
}
publicoverridevoidFeatureDeactivating(SPFeatureReceiverPropertiesproperties)
{
SPWebsite=properties.Feature.ParentasSPWeb;
SPListbankList=site.Lists["Bankholidays"];
stringReceiverAssembly="CalendarFeatureHandler,Version=1.0.0.0,Culture=neutral,PublicKeyToken=932f09495b91102d";
//LooptroughtheEventReceivers
for(inti=0;i<bankList.EventReceivers.Count;i++)
{
if(bankList.EventReceivers[i].Assembly.Equals(ReceiverAssembly))
{
//DeleteEventReceiver
bankList.EventReceivers[i].Delete();
}
}
}
publicoverridevoidFeatureInstalled(SPFeatureReceiverPropertiesproperties)
{
thrownewNotImplementedException();
}
publicoverridevoidFeatureUninstalling(SPFeatureReceiverPropertiesproperties)
{
thrownewNotImplementedException();
}
}
}



aaroh_bits  Saturday, January 10, 2009 7:34 AM

Hi,

the problem is that you are trying to update SPList object instead of each SPListItem object you changed.

SPList.Update updates list properties, but you didn't change any list property. You are setting list item properties, so you need to call Update on each list item you edit.

Here is the solution:

foreach(SPListItemiteminitems)
{
weekend=isWeekend(Convert.ToDateTime(item["Day"]));
item["Day"].ToString();
if(weekend)
{
SPListItemCollectionlc=teamList.GetItems(queryTeam);
foreach(SPListItemitem2inlc)
{
item2["TotalNumberOfLeaves"]=Convert.ToInt16(item2["TotalNumberOfLeaves"])+1;
//teamList.Update();//NOTUPDATINGTHELIST!!->WRONG
item2.Udate();//CORRECT!
}
}
}

Regards,
Sasa
Sasa Tomicic <--> MCTS WSS 3.0, MCPD EAD... www.teamcompanion.com
  • Marked As Answer byaaroh_bits Saturday, January 10, 2009 4:29 PM
  •  
Sasa Tomicic  Saturday, January 10, 2009 1:14 PM

Hi,

the problem is that you are trying to update SPList object instead of each SPListItem object you changed.

SPList.Update updates list properties, but you didn't change any list property. You are setting list item properties, so you need to call Update on each list item you edit.

Here is the solution:

foreach(SPListItemiteminitems)
{
weekend=isWeekend(Convert.ToDateTime(item["Day"]));
item["Day"].ToString();
if(weekend)
{
SPListItemCollectionlc=teamList.GetItems(queryTeam);
foreach(SPListItemitem2inlc)
{
item2["TotalNumberOfLeaves"]=Convert.ToInt16(item2["TotalNumberOfLeaves"])+1;
//teamList.Update();//NOTUPDATINGTHELIST!!->WRONG
item2.Udate();//CORRECT!
}
}
}

Regards,
Sasa
Sasa Tomicic <--> MCTS WSS 3.0, MCPD EAD... www.teamcompanion.com
  • Marked As Answer byaaroh_bits Saturday, January 10, 2009 4:29 PM
  •  
Sasa Tomicic  Saturday, January 10, 2009 1:14 PM
Hi sasa,

Thanks a million for your suggestion.
The code worked perfectly.

Have a great weekend.

Cheers,
aaroh
aaroh_bits  Saturday, January 10, 2009 4:29 PM

You can use google to search for other answers

Custom Search

More Threads

• Accesing to sharepoint web services anonymously from Silverlight
• vs2008 Server Error: Object reference not set to an instance of an object.
• How to access query parameters from a custom web part?
• Import List and Columns Problem
• subscription based communication functionality in MOSS.
• Subsite Creation by using SIte Template
• CQWP -> Query a list and show totals.
• Issue with EventHandler in SharePoint
• Customizing mySite to included list from other site collection
• "Could not load type" error?