Can someone PLEAE help me solving this issue.I'm new to VS.
I am trying to Read the (Day1-Day7) items in Sharepoint list and and write to all (Day1-Day7) fields that are blank. I will like to place a value of zero that field.
I have included the code. I don't get any errors but I can get it to write to the field. Please help.
Any assistance would be greatly appreciate.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Globalization;
namespace Job
{
class STJob: SPJobDefinition
{
public const string JobName = "SP Job";
public STJobbDefinition() : base() { }
public STJobDefinition(SPWebApplication webApp) :
base(JobName, webApp, null, SPJobLockType.Job)
{
Title = "Timer Job";
}
public override void Execute(Guid targetInstanceId)
{
SPWeb web = null;
try
{
//Get current Web App
SPWebApplication webApp = this.Parent as SPWebApplication;
//Get the Request List URL from the job's property bag
String PLURL = this.Properties["PLURL"].ToString();
SPList sPList = GetListByUrl(PLURL);
//Get the column headers
sPList.Fields.GetField("Day1").Title = sPList.Fields.GetField("Day2").Title;
sPList.Fields.GetField("Day1").Update();
sPList.Fields.GetField("Day2").Title = sPList.Fields.GetField("Day3").Title;
sPList.Fields.GetField("Day2").Update();
sPList.Fields.GetField("Day3").Title = sPList.Fields.GetField("Day4").Title;
sPList.Fields.GetField("Day3").Update();
sPList.Fields.GetField("Day4").Title = sPList.Fields.GetField("Day5").Title;
sPList.Fields.GetField("Day4").Update();
sPList.Fields.GetField("Day5").Title = sPList.Fields.GetField("Day6").Title;
sPList.Fields.GetField("Day5").Update();
sPList.Fields.GetField("Day6").Title = sPList.Fields.GetField("Day7").Title;
sPList.Fields.GetField("Day6").Update();
sPList.Fields.GetField("Day14").Update();
//Get current web
web = sPList.ParentWeb;
String rolloverString = web.AllProperties["roll_over_date"].ToString();
DateTime rolloverDate = Convert.ToDateTime(rolloverString);
DateTime newRolloverDate = rolloverDate.AddDays(5;
String firstDay = newRolloverDate.ToString("M/d");
sPList.Fields.GetField("Day7).Title = firstDay;
sPList.Fields.GetField("Day7).Update();
//Set the rollover date in the site"s property bag to the Day after
web.Properties["roll_over_date"] = newRolloverDate.ToString("M/d/yyyy");
web.Properties.Update();
//Rollover the values for each item in a list
SPListItemCollection itemsCollection = sPList.Items;
foreach (SPListItem item in itemsCollection)
{
item["Day1"] = item["Day2"];
item["Day2"] = item["Day3"];
item["Day3"] = item["Day4"];
item["Day4"] = item["Day5"];
item["Day5"] = item["Day6"];
//item["Day7] 0;
item.Update();
}
}
catch
{
throw;
}
finally
{
web.Dispose();
}
}
//Set Value to 0
public void getData()
{
//Get SPSite Object
string strUrl = "Name of site URL"
using (SPSite site = new SPSite(strUrl))
{
//Connect to the web using SPWeb object
using (SPWeb web = site.OpenWeb())
{
//List Object to get the list from a sharepoint site
SPList sPList = web.Lists["List Name"];
SPQuery query = new SPQuery();
query.Query = string.Concat("<Where>","<Contains>","<FieldRef Name='Day1'/>","<FieldRef Name='Day2'/>","<FieldRef Name='Day3'/>","<FieldRef Name='Day4'/>","<FieldRef Name='Day5'/>","<FieldRef Name='Day6'/>","<FieldRef Name='Day7'/>","<Value Type='Number'>","","</Value>","</Contains>","</Where>");
SPListItemCollection items = sPList.GetItems(query);
foreach (SPListItem item in items)
{
if (item != null)
{
SPListItemCollection listItems = web.Lists["List_Name"].Items;
SPListItem item_w = listItems.Add();
item_w["Day1"] = 3;
item_w["Day2"] = 4;
item_w["Day3"] = item_w["Day4"];
item_w["Day4"] = item_w["Day5"];
item_w["Day5"] = item_w["Day6"];
item_w["Day6"] = item_w["Day7"];
item_w["Day7"] = item_w["Day8"];
item_w.Update();
}
}
}
}
}
////
/// <summary>
/// Gets an SPList based on the url to the list
/// </summary>
private SPList GetListByUrl(string listUrl)
{
SPList list = null;
try
{
using (SPSite site = new SPSite(listUrl))
{
if (site != null)
{
// Strip off the site url, leaving the rest
// We'll use this to open the web
string webUrl = listUrl.Substring(site.Url.Length);
// Strip off anything after /forms/
int formsPos = webUrl.IndexOf("/forms/", 0, StringComparison.InvariantCultureIgnoreCase);
if (formsPos >= 0)
{
webUrl = webUrl.Substring(0, webUrl.LastIndexOf('/', formsPos));
}
// Strip off anything after /lists/
int listPos = webUrl.IndexOf("/lists/", 0, StringComparison.InvariantCultureIgnoreCase);
if (listPos >= 0)
{
// Must be a custom list
// Strip off anything after /lists/
webUrl = webUrl.Substring(0, webUrl.LastIndexOf('/', listPos));
}
else
{
// No lists, must be a document library.
// Strip off the document library name
webUrl = webUrl.Substring(0, webUrl.LastIndexOf('/'));
}
// Get the web site
using (SPWeb web = site.OpenWeb(webUrl))
{
if (web != null)
{
// Initialize the web (avoids COM exceptions)
string title = web.Title;
// Strip off the relative list Url
// Form the full path to the list
//string relativeListUrl = listUrl.Substring(web.Url.Length);
//string url = SPUrlUtility.CombineUrl(web.Url, relativeListUrl);
// Get the list
list = web.GetList(listUrl);
}
}
}
}
}
catch { throw; }
return list;
}
}
}