-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclsCommonBL.cs
129 lines (99 loc) · 3.87 KB
/
clsCommonBL.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Specialized;
using Microsoft.SharePoint;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
namespace SP.GlobalTopMenu
{
public static class clsCommonBL
{
public enum FindBy
{
BySiteId = 1,
BySiteUrl
};
/// <summary>
/// This method verifies if the user has permissions to access specific SubSite
/// </summary>
/// <param name="web">Sub Site where it is need to verify if the user has permissions</param>
/// <param name="userLoginName">UserName of the user</param>
/// <returns>True: User has permission to access the sub site. False: User does not have permissions to access the sub site</returns>
public static bool IsUserHasAccess(string SiteUrl, string userLoginName)
{
bool hasAccess = false;
SPBasePermissions sitePermissions = SPBasePermissions.ViewPages;
try
{
SPSecurity.RunWithElevatedPrivileges(
delegate()
{
if (SPContext.Current.Site.WebApplication.Sites[SiteUrl] != null)
{
using (SPWeb web = SPContext.Current.Site.WebApplication.Sites[SiteUrl].OpenWeb())
{
web.Site.CatchAccessDeniedException = false;
hasAccess = web.DoesUserHavePermissions(userLoginName, sitePermissions);
}
}
else
hasAccess = true;
});
}
catch
{
hasAccess = false;
}
return hasAccess;
}
/// <summary>
/// Converts Linq result to DataTable object.
/// </summary>
/// <typeparam name="T">Any type</typeparam>
/// <param name="varlist">LINQ Result</param>
/// <returns></returns>
public static DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
{
DataTable dtReturn = new DataTable();
try
{
// column names
PropertyInfo[] oProps = null;
if (varlist == null) return dtReturn;
foreach (T rec in varlist)
{
// Use reflection to get property names, to create table, Only first time, others will follow
if (oProps == null)
{
oProps = ((Type)rec.GetType()).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition()
== typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
if (pi.Name != "Item")
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}
DataRow dr = dtReturn.NewRow();
foreach (PropertyInfo pi in oProps)
{
if (pi.Name != "Item" && pi.Name != "Attachments")
dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null);
}
dtReturn.Rows.Add(dr);
}
}
catch (Exception ex)
{
throw;
}
return dtReturn;
}
}
}