Hi !
I have a web part code with no controls on it. This web part simply checks the current login user entry in a custom list. If found the user can access the home page else it redirects the user to a page where the user needs to accept the policy and code of conduct of company which is another web part.
Now when I insert the first web part on home page, after saving the page I am encountering with below error.
Error: This page contains content or formatting that is not valid. You can find more information in the affected sections
Below is the web part code. Please let me know what changes should I make below:
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace CheckPolicyAccept.CheckPolicyAccept
{
[ToolboxItemAttribute(false)]
public class CheckPolicy : WebPart
{
// Visual Studio might automatically update this path when you change the Visual Web Part project item.
private const string _ascxPath = @"~/_CONTROLTEMPLATES/CheckPolicyAccept/CheckPolicyAccept/CheckPolicyAccept.ascx";
protected override void CreateChildControls()
{
WebPartManager wp = WebPartManager.GetCurrentWebPartManager(this.Page);
if (wp.DisplayMode == WebPartManager.BrowseDisplayMode)
{
base.CreateChildControls();
Control control = Page.LoadControl(_ascxPath);
Controls.Add(control);
}
}
}
public partial class CheckPolicyAcceptUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
string url = string.Empty;
LogMessage(TraceSeverity.None,"Executing Check Policy");
try
{
if (IsPostBack != true)
{
Guid siteId = SPContext.Current.Site.ID;
Guid webId = SPContext.Current.Site.OpenWeb().ID;
SPUser spUser = SPContext.Current.Site.OpenWeb().CurrentUser;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
string currentLoginName = string.Empty;
string currentUserDisplayName = string.Empty;
using (SPSite spSite = new SPSite(siteId))
{
using (SPWeb spWeb = spSite.OpenWeb(webId))
{
currentLoginName = spUser.LoginName;
currentUserDisplayName = spUser.Name;
SPList spList = spWeb.Lists["Policy Acceptance Status"];
SPQuery spQuery = new SPQuery();
LogMessage(TraceSeverity.None, "Executing Policy Check for: " + currentLoginName);
spQuery.Query = "<Where><Eq><FieldRef Name='EmpName' /><Value Type='Text'>" + currentLoginName + "</Value></Eq></Where>";
SPListItemCollection spListItemCollection = spList.GetItems(spQuery);
if (spListItemCollection.Count == 0)
{
url = returnPolicyAcceptancePageURL();
LogMessage(TraceSeverity.None, "Check Policy User: " + currentLoginName + " now redirecting to URL: " + url);
if (!string.IsNullOrEmpty(url))
{
Response.Redirect(url, false);
}
}
}
}
});
}
}
catch (Exception ex)
{
LogMessage(ex);
}
}
public string returnPolicyAcceptancePageURL()
{
string siteURL = string.Empty;
Guid siteId = SPContext.Current.Site.ID;
Guid webId = SPContext.Current.Site.OpenWeb().ID;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite spSite = new SPSite(siteId);
SPWeb spWeb = spSite.OpenWeb(webId);
SPList spList = spWeb.Lists["RedirectUrl"];
SPListItemCollection itemColl = spList.Items;
foreach (SPListItem item in itemColl)
{
if (Convert.ToString(item["Title"]) == "Policy Acceptance")
{
siteURL = Convert.ToString(item["Url"]);
}
}
});
return siteURL;
}
private void LogMessage(TraceSeverity severity,string message)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
uint uintEventID = 700;//event ID
string CategoryName = "Check Policy Webpart";
SPDiagnosticsCategory category = new SPDiagnosticsCategory(CategoryName, TraceSeverity.Medium, EventSeverity.Error);
SPDiagnosticsService.Local.WriteTrace(uintEventID, category, TraceSeverity.Unexpected, message);
});
}
private void LogMessage(Exception ex)
{
LogMessage(TraceSeverity.High, ex.Message + ex.StackTrace);
}
}
}