SharePoint Project 验证填写内容(比对数据SharePoint list)
using Microsoft.SharePoint;
using Microsoft.ProjectServer.Client;
using Microsoft.SharePoint.Client;
using System.Configuration;
using System.Web.Script.Serialization;
namespace LSPMP_PWA_Services
{
/// <summary>
/// ChangeProject 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
public class ChangeProject : System.Web.Services.WebService
{
//设置站点
private static ProjectContext projectContext()
{
string url = ConfigurationManager.AppSettings["HOST"];
var context = new ProjectContext(url);
return context;
}
[WebMethod]
public void ChangeTasks(string projects, string callback)
{
string result = string.Empty;
string message = string.Empty;
StringBuilder strDraft = new StringBuilder();
StringBuilder strPub = new StringBuilder();
try
{
SPSecurity.RunWithElevatedPrivileges(delegate ()
{
#region 读取草稿库信息-数据源来自于SP list
strDraft.AppendLine(" SELECT");
strDraft.AppendLine(" TASK_UID");
strDraft.AppendLine(" ,TASK_NAME");
strDraft.AppendLine(" ,TASK_START_DATE");
strDraft.AppendLine(" ,TASK_FINISH_DATE");
strDraft.AppendLine(" FROM");
strDraft.AppendLine(" WSS_Content.pjdraft.MSP_TASKS");
strDraft.AppendLine(" WHERE");
strDraft.AppendLine(" PROJ_UID = '" + projects + "'");
strDraft.AppendLine(" AND");
strDraft.AppendLine(" TASK_OUTLINE_LEVEL <= 3");
strDraft.AppendLine(" AND");
strDraft.AppendLine(" TASK_NAME IS NOT NULL");
DataTable dtDraft = DBHelper.GetDataTable(strDraft.ToString());
#endregion
#region 读取发布库信息-数据源SPProject
strPub.AppendLine(" SELECT");
strPub.AppendLine(" TASK_UID");
strPub.AppendLine(" ,TASK_NAME");
strPub.AppendLine(" ,TASK_START_DATE");
strPub.AppendLine(" ,TASK_FINISH_DATE");
strPub.AppendLine(" FROM");
strPub.AppendLine(" WSS_Content.pjpub.MSP_TASKS");
strPub.AppendLine(" WHERE");
strPub.AppendLine(" PROJ_UID = '" + projects + "'");
strPub.AppendLine(" AND");
strPub.AppendLine(" TASK_OUTLINE_LEVEL <= 3");
strPub.AppendLine(" AND");
strPub.AppendLine(" TASK_NAME IS NOT NULL");
DataTable dtPub = DBHelper.GetDataTable(strPub.ToString());
#endregion
if (dtDraft.Rows.Count == dtPub.Rows.Count)
{
for (int i = 0; i < dtDraft.Rows.Count; i++)
{
DataRow dr = GetTaskList(projects, Convert.ToString(dtDraft.Rows[i]["TASK_UID"]));
if (dr != null)
{
if (Convert.ToString(dtDraft.Rows[i]["TASK_NAME"]) == Convert.ToString(dr["TASK_NAME"]))
{
if (Convert.ToDateTime(dtDraft.Rows[i]["TASK_START_DATE"].ToString()) >= Convert.ToDateTime(dr["TASK_START_DATE"].ToString()) && Convert.ToDateTime(dtDraft.Rows[i]["TASK_FINISH_DATE"].ToString()) <= Convert.ToDateTime(dr["TASK_FINISH_DATE"].ToString()))
{
message = "恭喜您,项目发布成功";
result = string.Format("{0}({1})", callback, new JavaScriptSerializer().Serialize(message));
}
else
{
message = "由于您更改了" + Convert.ToString(dr["TASK_NAME"]) + "的任务时间范围,所以导致项目不能正常发布(原始时间范围:" + Convert.ToString(dr["TASK_START_DATE"]) + "——" + Convert.ToString(dr["TASK_FINISH_DATE"]) + ")";
result = string.Format("{0}({1})", callback, new JavaScriptSerializer().Serialize(message));
Context.Response.Write(result);
Context.Response.End();
}
}
else
{
message = "由于您更改了" + Convert.ToString(dr["TASK_NAME"]) + "的名称,所以导致项目不能正常发布";
result = string.Format("{0}({1})", callback, new JavaScriptSerializer().Serialize(message));
Context.Response.Write(result);
Context.Response.End();
}
}
else
{
message = "由于您添加未知任务(" + Convert.ToString(dr["TASK_NAME"]) + "),所以导致项目不能正常发布";
result = string.Format("{0}({1})", callback, new JavaScriptSerializer().Serialize(message));
Context.Response.Write(result);
Context.Response.End();
}
}
//发布事件
pubProj(projects);
Context.Response.Write(result);
Context.Response.End();
}
else
{
message = "您好,系统拒绝添加或删除任务";
result = string.Format("{0}({1})", callback, new JavaScriptSerializer().Serialize(message));
Context.Response.Write(result);
Context.Response.End();
}
});
}
catch (Exception ex) { }
}
[WebMethod]
public void ChangeTB(string projects, string callback)
{
string result = string.Empty;
string message = string.Empty;
StringBuilder strDraft = new StringBuilder();
StringBuilder strPub = new StringBuilder();
try
{
SPSecurity.RunWithElevatedPrivileges(delegate ()
{
#region 读取草稿库信息
strDraft.AppendLine(" SELECT");
strDraft.AppendLine(" TASK_UID");
strDraft.AppendLine(" ,TASK_NAME");
strDraft.AppendLine(" ,TASK_START_DATE");
strDraft.AppendLine(" ,TASK_FINISH_DATE");
strDraft.AppendLine(" FROM");
strDraft.AppendLine(" WSS_Content.pjdraft.MSP_TASKS");
strDraft.AppendLine(" WHERE");
strDraft.AppendLine(" PROJ_UID = '" + projects + "'");
strDraft.AppendLine(" AND");
strDraft.AppendLine(" TASK_OUTLINE_LEVEL <= 3");
strDraft.AppendLine(" AND");
strDraft.AppendLine(" TASK_NAME IS NOT NULL");
DataTable dtDraft = DBHelper.GetDataTable(strDraft.ToString());
#endregion
#region 读取发布库信息
strPub.AppendLine(" SELECT");
strPub.AppendLine(" TASK_UID");
strPub.AppendLine(" ,TASK_NAME");
strPub.AppendLine(" ,TASK_START_DATE");
strPub.AppendLine(" ,TASK_FINISH_DATE");
strPub.AppendLine(" FROM");
strPub.AppendLine(" WSS_Content.pjpub.MSP_TASKS");
strPub.AppendLine(" WHERE");
strPub.AppendLine(" PROJ_UID = '" + projects + "'");
strPub.AppendLine(" AND");
strPub.AppendLine(" TASK_OUTLINE_LEVEL <= 3");
strPub.AppendLine(" AND");
strPub.AppendLine(" TASK_NAME IS NOT NULL");
DataTable dtPub = DBHelper.GetDataTable(strPub.ToString());
#endregion
if (dtDraft.Rows.Count == dtPub.Rows.Count)
{
for (int i = 0; i < dtDraft.Rows.Count; i++)
{
DataRow dr = GetTaskList(projects, Convert.ToString(dtDraft.Rows[i]["TASK_UID"]));
if (dr != null)
{
if (Convert.ToString(dtDraft.Rows[i]["TASK_NAME"]) == Convert.ToString(dr["TASK_NAME"]))
{
if (Convert.ToDateTime(dtDraft.Rows[i]["TASK_START_DATE"].ToString()) >= Convert.ToDateTime(dr["TASK_START_DATE"].ToString()) && Convert.ToDateTime(dtDraft.Rows[i]["TASK_FINISH_DATE"].ToString()) <= Convert.ToDateTime(dr["TASK_FINISH_DATE"].ToString()))
{
if (Convert.ToInt32(DraftTB(projects)) > Convert.ToInt32(PubTB(projects)))
{
message = "恭喜您,项目发布成功";
result = string.Format("{0}({1})", callback, new JavaScriptSerializer().Serialize(message));
}
else
{
message = "项目发布之前请先设置基线";
result = string.Format("{0}({1})", callback, new JavaScriptSerializer().Serialize(message));
Context.Response.Write(result);
Context.Response.End();
}
}
else
{
message = "由于您更改了" + Convert.ToString(dr["TASK_NAME"]) + "的任务时间范围,所以导致项目不能正常发布(原始时间范围:" + Convert.ToString(dr["TASK_START_DATE"]) + "——" + Convert.ToString(dr["TASK_FINISH_DATE"]) + ")";
result = string.Format("{0}({1})", callback, new JavaScriptSerializer().Serialize(message));
Context.Response.Write(result);
Context.Response.End();
}
}
else
{
message = "由于您更改了" + Convert.ToString(dr["TASK_NAME"]) + "的名称,所以导致项目不能正常发布";
result = string.Format("{0}({1})", callback, new JavaScriptSerializer().Serialize(message));
Context.Response.Write(result);
Context.Response.End();
}
}
else
{
message = "由于您添加未知任务(" + Convert.ToString(dr["TASK_NAME"]) + "),所以导致项目不能正常发布";
result = string.Format("{0}({1})", callback, new JavaScriptSerializer().Serialize(message));
Context.Response.Write(result);
Context.Response.End();
}
}
//发布事件
pubProj(projects);
Context.Response.Write(result);
Context.Response.End();
}
else
{
message = "您好,系统拒绝添加或删除任务";
result = string.Format("{0}({1})", callback, new JavaScriptSerializer().Serialize(message));
Context.Response.Write(result);
Context.Response.End();
}
});
}
catch (Exception ex) { }
}
private DataRow GetTaskList(string project, string task)
{
StringBuilder str = new StringBuilder();
DataRow dr = null;
str.AppendLine(" SELECT");
str.AppendLine(" TASK_NAME");
str.AppendLine(" ,TASK_START_DATE");
str.AppendLine(" ,TASK_FINISH_DATE");
str.AppendLine(" FROM");
str.AppendLine(" WSS_Content.pjpub.MSP_TASKS");
str.AppendLine(" WHERE");
str.AppendLine(" PROJ_UID = '" + project + "'");
str.AppendLine(" AND");
str.AppendLine(" TASK_UID = '" + task + "'");
str.AppendLine(" AND");
str.AppendLine(" TASK_OUTLINE_LEVEL <= 3");
str.AppendLine(" AND");
str.AppendLine(" TASK_NAME IS NOT NULL");
DataTable dtPub = DBHelper.GetDataTable(str.ToString());
if (dtPub.Rows.Count > 0)
{
dr = dtPub.Rows[0];
}
return dr;
}
private string DraftTB(string projectUid)
{
string result = string.Empty;
StringBuilder str = new StringBuilder();
str.AppendLine(" SELECT");
str.AppendLine(" TOP(1)");
str.AppendLine(" MOD_REV_COUNTER");
str.AppendLine(" FROM");
str.AppendLine(" WSS_Content.pjdraft.MSP_TASK_BASELINES");
str.AppendLine(" WHERE");
str.AppendLine(" PROJ_UID = '" + projectUid + "'");
str.AppendLine(" ORDER BY MOD_REV_COUNTER DESC");
DataTable dt = DBHelper.GetDataTable(str.ToString());
result = Convert.ToString(dt.Rows[0]["MOD_REV_COUNTER"]);
return result;
}
private string PubTB(string projectUid)
{
string result = string.Empty;
StringBuilder str = new StringBuilder();
str.AppendLine(" SELECT");
str.AppendLine(" TOP(1)");
str.AppendLine(" MOD_REV_COUNTER");
str.AppendLine(" FROM");
str.AppendLine(" WSS_Content.pjpub.MSP_TASK_BASELINES");
str.AppendLine(" WHERE");
str.AppendLine(" PROJ_UID = '" + projectUid + "'");
str.AppendLine(" ORDER BY MOD_REV_COUNTER DESC");
DataTable dt = DBHelper.GetDataTable(str.ToString());
result = Convert.ToString(dt.Rows[0]["MOD_REV_COUNTER"]);
return result;
}
private static void pubProj(string projectUid)
{
var projContext = projectContext();
var projCollection = projContext.LoadQuery(projContext.Projects.Where(p => p.Id == new Guid(projectUid)));
projContext.ExecuteQuery();
PublishedProject proj2Edit = projCollection.First();
DraftProject projCheckedOut = proj2Edit.Draft;
projContext.Load(projCheckedOut);
projContext.ExecuteQuery();
projContext.Load(projCheckedOut.Tasks);
projContext.ExecuteQuery();
JobState jobState_1 = projContext.WaitForQueue(projCheckedOut.Update(), 20);
if (jobState_1 == JobState.Success)
{
projContext.Load(projCheckedOut);
projContext.ExecuteQuery();
projContext.Projects.Update();
JobState jobState_2 = projContext.WaitForQueue(projContext.Projects.Update(), 20);
if (jobState_2 == JobState.Success)
{
projContext.ExecuteQuery();
projCheckedOut.Publish(false);
JobState jobState_3 = projContext.WaitForQueue(projContext.Projects.Update(), 20);
if (jobState_3 == JobState.Success)
{
QueueJob qJob2 = projCheckedOut.CheckIn(true);
projContext.Load(qJob2);
projContext.ExecuteQuery();
JobState jobState2 = projContext.WaitForQueue(qJob2, 20);
}
}
}
}
}
}