If we want to generate values in one dropdown field dynamically in the EditForm.aspx of Library comparing to some value in other list.
Here in the following example we create a list having Username & Department columns.
Also create a library having the SubDepartment field as dropdown field which should show values according to the department of the current user in UsersDepartment list.
<script runat="server">
protected void Page_Load(Object sender, EventArgs e)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
// Elevated Permission Code Goes Here
using (SPSite spSite = SPContext.Current.Site)
{
using (SPWeb web = spSite.OpenWeb())
{
//Read List & Library
SPList DepartmentList = web.Lists["UsersDepartment"];
SPList MyDocumentList = web.Lists["MyDocumentLibrary"];
//Read SubDepartment field from Library
SPFieldChoice choicefield = (SPFieldChoice)MyDocumentList.Fields["SubDepartment"];
SPQuery query = new SPQuery();
//Get Current User
SPUser currentuser =SPContext.Current.Web.CurrentUser;
string strcurrentuser = currentuser.LoginName.ToString();
char[] arrSplit={'\\'};
query.Query = @"<Where><Eq><FieldRef Name='User_x0020_Name' /><Value Type='Text'>" + strcurrentuser.Split(arrSplit)[1] + @"</Value></Eq></Where>"; //Fetch listitems from DepartmentList
SPListItemCollection collListItem = DepartmentList.GetItems(query);
web.AllowUnsafeUpdates = true;
if(collListItem.Count > 0)
{
string department =collListItem[0]["Department"].ToString();
if(department !="") {
if(department == "Science")
{
choicefield.Choices.Clear();
choicefield.Choices.Add("Select Department");
choicefield.Choices.Add("Physics");
choicefield.Choices.Add("Chemistry");
choicefield.Choices.Add("Biology");
choicefield.Hidden=false;
choicefield.Required=true;
choicefield.Update();
}
else if(department == "Commerce")
choicefield.Choices.Clear();
choicefield.Choices.Add("Select Department");
choicefield.Choices.Add("Accounts");
choicefield.Choices.Add("Marketing");
choicefield.Hidden=false;
choicefield.Required=true;
choicefield.Update();
}
}
else
{
choicefield.Hidden=true;
choicefield.Required=false;
choicefield.Update();
}
}
else
{
choicefield.Hidden=true;
choicefield.Required=false;
choicefield.Update();
}
MyDocumentList.Update();
web.AllowUnsafeUpdates = false;
}
}
});
}
</script>
In the above code if the department of the current user is "Science" the values populated in the dropdown will be "Physics", "Chemistry" & "Biology".
This code runs at server side.
Copy the above code on the page where the above document library is rendered using SharePoint Designer.
Here in the following example we create a list having Username & Department columns.
Also create a library having the SubDepartment field as dropdown field which should show values according to the department of the current user in UsersDepartment list.
<script runat="server">
protected void Page_Load(Object sender, EventArgs e)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
// Elevated Permission Code Goes Here
using (SPSite spSite = SPContext.Current.Site)
{
using (SPWeb web = spSite.OpenWeb())
{
//Read List & Library
SPList DepartmentList = web.Lists["UsersDepartment"];
SPList MyDocumentList = web.Lists["MyDocumentLibrary"];
//Read SubDepartment field from Library
SPFieldChoice choicefield = (SPFieldChoice)MyDocumentList.Fields["SubDepartment"];
SPQuery query = new SPQuery();
//Get Current User
SPUser currentuser =SPContext.Current.Web.CurrentUser;
string strcurrentuser = currentuser.LoginName.ToString();
char[] arrSplit={'\\'};
query.Query = @"<Where><Eq><FieldRef Name='User_x0020_Name' /><Value Type='Text'>" + strcurrentuser.Split(arrSplit)[1] + @"</Value></Eq></Where>"; //Fetch listitems from DepartmentList
SPListItemCollection collListItem = DepartmentList.GetItems(query);
web.AllowUnsafeUpdates = true;
if(collListItem.Count > 0)
{
string department =collListItem[0]["Department"].ToString();
if(department !="") {
if(department == "Science")
{
choicefield.Choices.Clear();
choicefield.Choices.Add("Select Department");
choicefield.Choices.Add("Physics");
choicefield.Choices.Add("Chemistry");
choicefield.Choices.Add("Biology");
choicefield.Hidden=false;
choicefield.Required=true;
choicefield.Update();
}
else if(department == "Commerce")
choicefield.Choices.Clear();
choicefield.Choices.Add("Select Department");
choicefield.Choices.Add("Accounts");
choicefield.Choices.Add("Marketing");
choicefield.Hidden=false;
choicefield.Required=true;
choicefield.Update();
}
}
else
{
choicefield.Hidden=true;
choicefield.Required=false;
choicefield.Update();
}
}
else
{
choicefield.Hidden=true;
choicefield.Required=false;
choicefield.Update();
}
MyDocumentList.Update();
web.AllowUnsafeUpdates = false;
}
}
});
}
</script>
In the above code if the department of the current user is "Science" the values populated in the dropdown will be "Physics", "Chemistry" & "Biology".
This code runs at server side.
Copy the above code on the page where the above document library is rendered using SharePoint Designer.
No comments:
Post a Comment