Logo 
Search:

Sharepoint Forum

Ask Question   UnAnswered
Home » Forum » Sharepoint       RSS Feeds

Repost for bulletin board in Sharepoint : Change Expiry Date

  Asked By: Jay    Date: May 22    Category: Sharepoint    Views: 2693

As i sent a previous post, hope the way i was approaching the
solution ain't right.
I have implemented an online bulletin board with sharepoint where
people can send their posts. After 30 days the post get expired and
will be still visible in there private list. All i want to do is to
write a webpart to change the ExpiryDate with adding 30 days which is
something like a Repost thing. All this is done with content
approval. So here is the code that i tried with, (only the main
snippet)

private void createConn()
{

try
{

// Put user code to initialize the page here
this.Title = "Repost Webpart for bulletin board";

SPSite site = new SPSite("http://mainserver");
SPWeb web = site.AllWebs["sites/mysite"];
SPList list = web.Lists["bulletinboard"];
SPQuery query = new SPQuery();

int ID = int.Parse(Context.Request.Params["ID"]);

if (DateTime.Compare(CurrentDate,ExpiryDate) <= 0 )
{
query.Query = "<Where><Eq><FieldRef Name='ID'/><Value
Type='Number'>"+ID+"</Value></Eq></Where>";

SPListItemCollection listItems = list.GetItems(query);

foreach (SPListItem listItems1 in listItems)
{
ExpiryDate = Convert.ToDateTime(listItems1["ExpiryDate"]);
CurrentDate = DateTime.Now;
listItems1["ExpiryDate"] = CurrentDate.AddMonths(1);
listItems1.Update();
Context.Response.Write(listItems1["CreatedBy"].ToString());
}
}

else
{
Context.Response.Redirect
("mainserver/sites/mysite/bulletinboard/error.aspx");
}

}
catch(Exception mess)
{
error.Text = mess.ToString();
}
}


This i am trying to achieve by sending the user to the webpart page
where the above webpart is. I am totally a new guy to C# and the
above im trying to build from a scratch. It seems the above code aint
working. Anybody have any idea? Let me know if there is a better way
to achieve this.

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Paola Mcmahon     Answered On: May 22

Sorry guys, i was happened to paste the wrong code  in the post, the
correct code im using is as follows,

//Method establishes the connection and compares the dates

private void createConn()
{
try
{
// Put user  code to initialize the page  here
this.Title = "Repost Webpart for bulletinboard";

int ID = int.Parse(Context.Request.Params["ID"]);

SPSite site  = new SPSite("http://mainserver");
SPWeb web  = site.AllWebs["sites/mysite"];
SPList list  = web.Lists["bulletinboard"];
SPListItem listItem = list.GetItemById(ID);

ExpiryDate = DateTime.Parse(listItem["ExpiryDate"].ToString
());
CurrentDate = DateTime.Now;

if (DateTime.Compare(CurrentDate,ExpiryDate) <= 0 )
{
listItem["ExpiryDate"] = CurrentDate.AddMonths
(1);
listItem.Update();
}
else
{
Context.Response.Redirect
("mainserver/sites/mysite/bulletinboard/Default.aspx");
}


}
catch(Exception mess)
{
Context.Response.Write(mess.ToString());
}
}

 




Tagged: