SharePoint 2013 Training in Bangalore, SharePoint 2013 Developer Training, SharePoint 2013 Administration Training, Best SharePoint 2013 Training in Bangalore, Best Sharepoint Training in Bangalore, SharePoint 2013 Developer Training in Bangalore, Sharepoint Interview Questions, Sharepoint Real Time Interview Questions, Sharepoint Programming interview questions, Sharepoint 2010 interview questions, sharepoint 2013 interview questions
SharePoint Programming Common Methods For Lists
public class ManageList
{
public void UpdateListItem(Guid siteID, string webName, SPListItem listItem, string[] colNames)
{
try
{
if (colNames.Length > 0)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite site = new SPSite(siteID);
using (SPWeb web = site.OpenWeb(webName))
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists[listItem.ParentList.ID];
SPListItem item = list.Items[listItem.UniqueId];
for (int colNum = 0; colNum < colNames.Length; colNum++)
{
item[colNames[colNum]] = listItem[colNames[colNum]];
}
item.Update();
web.AllowUnsafeUpdates = false;
}
});
}
}
catch (Exception ex) { throw new SPException(ex.Message.ToString()); }
}
// Function to Insert new ListItem
public void InsertNewListItem(Guid siteID, string webName, string listName, string[] colNames, string[] colValues)
{
try
{
SPListItem item = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite site = new SPSite(siteID);
using (SPWeb web = site.OpenWeb(webName))
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists[listName];
item = list.Items.Add();
for (int i = 0; i < colNames.Length; i++)
{
if (list.Fields.GetFieldByInternalName(colNames[i]).Type.ToString() == "DateTime")
{
if (DateTime.Parse(colValues[i]) != DateTime.MinValue)
item[colNames[i]] = DateTime.Parse(colValues[i]);
}
else
item[colNames[i]] = colValues[i];
}
item.Update();
web.AllowUnsafeUpdates = false;
}
});
}
catch (Exception ex) { throw new SPException(ex.Message.ToString()); }
}
/// <summary>
/// Function to Insert new ListItem
/// </summary>
public SPListItem InsertNewListItem(Guid siteID, string webName, string listName)
{
try
{
SPListItem item = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite site = new SPSite(siteID);
using (SPWeb web = site.OpenWeb(webName))
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists[listName];
item = list.Items.Add();
item.Update();
web.AllowUnsafeUpdates = false;
}
});
return item;
}
catch (Exception ex) { throw new SPException(ex.Message.ToString()); }
}
/// <summary>
/// Function to Delete ListItem
/// </summary>
public void DeleteListItem(Guid siteID, string webName, SPListItem listItem)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite site = new SPSite(siteID);
using (SPWeb web = site.OpenWeb(webName))
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists[listItem.ParentList.ID];
SPListItem item = list.Items[listItem.UniqueId];
item.Delete();
web.AllowUnsafeUpdates = false;
}
});
}
catch (Exception ex) { throw new SPException(ex.Message.ToString()); }
}
public void UpdateItem(Guid siteID, string webName, SPListItem listItem, System.IO.Stream fStream, string fname)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite site = new SPSite(siteID);
using (SPWeb web = site.OpenWeb(webName))
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists[listItem.ParentList.ID];
SPListItem item = list.Items[listItem.UniqueId];
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
fStream.Dispose();
SPAttachmentCollection attachments = item.Attachments;
string fileName = System.IO.Path.GetFileName(fname);
attachments.Add(fileName, contents);
item.Update();
web.AllowUnsafeUpdates = false;
}
});
}
catch (Exception ex) { throw new SPException(ex.Message.ToString()); }
}
///Delete Attached Items
public void DeleteAttachItem(Guid siteID, string webName, string listName, SPListItem item)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPFileCollection files = null;
SPFolder itemFolder = null;
SPSite site = new SPSite(siteID);
using (SPWeb web = site.OpenWeb(webName))
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists[listName];
string strAttchmentFolderURL = item.Attachments.UrlPrefix;
if (list.RootFolder.SubFolders.Count > 0)
{
if (list.RootFolder.SubFolders["Attachments"] != null)
{
itemFolder = list.RootFolder.SubFolders["Attachments"];
}
}
if (itemFolder.SubFolders[strAttchmentFolderURL] != null)
{
files = itemFolder.SubFolders[strAttchmentFolderURL].Files;
}
foreach (SPFile file in files)
{
//Delete the file finally
file.Delete();
break;
}
web.AllowUnsafeUpdates = false;
}
});
}
catch (Exception ex) { throw new SPException(ex.Message.ToString()); }
}
public string AttachFileName(Guid siteID, string webName, string listName, SPListItem item)
{
string url = null;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPFileCollection files = null;
SPFolder itemFolder = null;
SPSite site = new SPSite(siteID);
using (SPWeb web = site.OpenWeb(webName))
{
web.AllowUnsafeUpdates = true;
SPList list = web.Lists[listName];
string strAttchmentFolderURL = item.Attachments.UrlPrefix;
if (list.RootFolder.SubFolders.Count > 0)
{
if (list.RootFolder.SubFolders["Attachments"] != null)
{
itemFolder = list.RootFolder.SubFolders["Attachments"];
}
}
if (itemFolder.SubFolders[strAttchmentFolderURL] != null)
{
files = itemFolder.SubFolders[strAttchmentFolderURL].Files;
}
foreach (SPFile file in files)
{
//Delete the file finally
url=file.Url;
}
web.AllowUnsafeUpdates = false;
}
});
}
catch (Exception ex) { throw new SPException(ex.Message.ToString()); }
return url;
}
}
public DataTable GetAllListData(Guid siteID, string webName, string listName)
{
DataTable dtResult = null;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite site = new SPSite(siteID);
using (SPWeb web = site.OpenWeb(webName))
{
SPList list = web.Lists[listName];
dtResult = list.Items.GetDataTable();
}
});
}
catch { }
return dtResult;
}
/// <summary>
/// Function to get ListData with all columns using CAML query
/// </summary>
public DataTable GetListData(Guid siteID, string webName, string listName, string listQuery)
{
DataTable dtResult = null;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite site = new SPSite(siteID);
using (SPWeb web = site.OpenWeb(webName))
{
SPQuery query = new SPQuery();
query.Query = listQuery;
SPList list = web.Lists[listName];
dtResult = list.GetItems(query).GetDataTable();
}
});
}
catch { }
return dtResult;
}
/// <summary>
/// Function to get ListData with named columns using CAML query
/// </summary>
///
public DataTable GetListData(Guid siteID, string webName, string listName, string listQuery, string queryCols)
{
DataTable dtResult = null;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite site = new SPSite(siteID);
using (SPWeb web = site.OpenWeb(webName))
{
SPQuery query = new SPQuery();
query.ViewFields = queryCols;
query.Query = listQuery;
SPList list = web.Lists[listName];
dtResult = list.GetItems(query).GetDataTable();
}
});
}
catch { }
return dtResult;
}
/// <summary>
/// Function to get ListItemCollection using CAML query
/// </summary>
///
public SPListItemCollection GetListItemCollection(Guid siteID, string webName, string listName, string listQuery)
{
SPListItemCollection itemColl = null;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite site = new SPSite(siteID);
using (SPWeb web = site.OpenWeb(webName))
{
SPQuery query = new SPQuery();
query.Query = listQuery;
SPList list = web.Lists[listName];
itemColl = list.GetItems(query);
}
});
}
catch { } // (Exception ex) { throw new SPException(ex.Message.ToString()); }
return itemColl;
}
/// <summary>
/// Function to get ListItem using CAML query
/// </summary>
public SPListItem GetListItem(Guid siteID, string webName, string listName, string listQuery)
{
SPListItem item = null;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite site = new SPSite(siteID);
using (SPWeb web = site.OpenWeb(webName))
{
SPQuery query = new SPQuery();
query.Query = listQuery;
SPList list = web.Lists[listName];
SPListItemCollection itemColl = list.GetItems(query);
if (itemColl.Count > 0)
{
item = itemColl[0];
}
}
});
}
catch { }
return item;
}
public String[] GetUser_Role_ID(Guid siteID, string loginName)
{
string role = ""; string name = ""; string id = "";
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite gmtfSite = new SPSite(siteID);
SPWeb gmtfWeb = gmtfSite.OpenWeb();
SPUser user = null;
try
{
user = gmtfWeb.AllUsers[loginName];
name = user.Name;
id = user.ID.ToString();
}
catch(Exception ex)
{
}
if (user != null)
{
if (user.IsSiteAdmin)
{
role = "Full Control";
}
else
{
role = "site user";
}
}
});
string[] user_Role_Name = { role, name, id };
return user_Role_Name;
}
catch (Exception ex) { throw new SPException(ex.Message.ToString()); }
}
public String GetUserPermission(Guid siteID, string loginName)
{
string role = "";
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite ctrmSite = new SPSite(siteID);
SPWeb ctrmWeb = ctrmSite.OpenWeb();
SPUser user = ctrmWeb.AllUsers[loginName];
if (user != null)
{
if (user.IsSiteAdmin)
role = "Full Control";
else
{
for (int i = 0; i < user.Groups.Count; i++)
{
try
{
if (user.Groups[i].Roles[0].Name == "Full Control")
{ role = "Full Control"; break; }
else if (user.Groups[i].Roles[0].Name == "Design")
{
if (role != "Full Control")
role = "Design";
}
else if (user.Groups[i].Roles[0].Name == "Contribute")
{
if (role != "Full Control" || role != "Design")
role = "Contribute";
}
else if (user.Groups[i].Roles[0].Name == "Read")
{
if (role != "Full Control" || role != "Design" || role != "Contribute")
role = "Read";
}
else if (user.Groups[i].Roles[0].Name == "View Only")
{
if (role != "Full Control" || role != "Design" || role != "Contribute" || role != "Read")
role = "View Only";
}
}
catch { }
}
}
}
});
return role;
}
catch (Exception ex) { throw new SPException(ex.Message.ToString()); }
}
public String[] GetUser_Role_ID(Guid siteID, string loginName)
{
string role = ""; string name = ""; string id = "";
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite Site = new SPSite(siteID);
SPWeb Web = Site.OpenWeb();
SPUser user = null;
try
{
user = Web.AllUsers[loginName];
name = user.Name;
id = user.ID.ToString();
}
catch
{
}
if (user != null)
{
if (user.IsSiteAdmin)
{
role = "Full Control";
}
else
{
role = "site user";
}
}
});
string[] user_Role_Name = { role, name, id };
return user_Role_Name;
}
catch (Exception ex) { throw new SPException(ex.Message.ToString()); }
}
public SPUser GetUser(Guid siteID, int id, string userName)
{
SPUser user = null;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite ctrmSite = new SPSite(siteID);
SPWeb ctrmWeb = ctrmSite.OpenWeb();
if (id > 0)
user = ctrmWeb.AllUsers.GetByID(id);
else
user = ctrmWeb.EnsureUser(userName);
ctrmWeb.Dispose();
ctrmSite.Dispose();
});
}
catch (Exception ex)
{
}
return user;
}
public void AddUserToGroup(Guid siteID, SPUser user, string role)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPSite ctrmSite = new SPSite(siteID);
SPWeb ctrmWeb = ctrmSite.OpenWeb();
SPGroup group = null;
if (role == "Admin")
group = ctrmWeb.Groups["SIte Members"];
else if (role == "User")
group = ctrmWeb.Groups["Site Visitors"];
if (group != null)
group.AddUser(user);
ctrmSite.Dispose();
ctrmWeb.Dispose();
});
}
public string GetSubString(string str, int strLength)
{
int i = strLength;
if (str.Length > i)
{
char[] c = str.ToCharArray();
if (c[strLength].ToString() != " " || c[strLength].ToString() != ".")
{
for (i = strLength; i > 0; i++)
{
if (i >= str.Length) break;
if (c[i].ToString() == " " || c[i].ToString() == ".") break;
}
}
str = str.Substring(0, i);
}
return str;
}
Subscribe to:
Posts (Atom)
No comments:
Post a Comment