Showing posts with label Object Model Coding. Show all posts
Showing posts with label Object Model Coding. Show all posts

Get multiple users from PeopleEditor and update column of type “People and Group” in SharePoint List

Get multiple users from PeopleEditor and update column of type “People and Group”.

Aspx Page:
<%@ Register TagPrefix="wssawc" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c" %>

<wssawc:peopleeditor id="UserPickerRequestedFor" runat="server" allowempty="False"
height="20px" multiselect="true" selectionset="User" showcreatebuttoninactivedirectoryaccountcreationmode="True"
validatorenabled="True" width="300px"></wssawc:peopleeditor>

Aspx.Cs Page:

ArrayList arrUsers = new ArrayList();
PickerEntity UserEntity = new PickerEntity();
SPFieldUserValueCollection fieldUsrValues = new SPFieldUserValueCollection();                  
int UsrCount = UserPicker.Accounts.Count;
for (int j = 0; j < UsrCount; j++)
{
    UserEntity = (PickerEntity) UserPicker.Entities[j];
    string  UserAcctName = UserEntity.Description;
    Hashtable PeopleEditorData = UserEntity.EntityData;
    string strUserLogin = UserEntity.Key;
    arrUsers.Add(strUserLogin);
}
foreach (object obj in arrUsers)
{
   string login = obj.ToString();
   SPSecurity.RunWithElevatedPrivileges(delegate()
   {
    using (SPSite elevatedSite = new SPSite(web.Site.ID))
    {
      elevatedSite.AllowUnsafeUpdates = true;
      SPWeb elevatedWeb = elevatedSite.OpenWeb(web.ID);
      {
        elevatedWeb.AllowUnsafeUpdates = true;
        SPUser User = elevatedWeb.EnsureUser(login);
        SPFieldUserValue val = new SPFieldUserValue(web,User.ID, User.Name);
        fieldUserValues.Add(val);
       }
      }
     });
   }
reqItem["UserNames"] = fieldUserValues;

Get multiple users from People Editor and update column of type “People and Group” – Method2

if (UserPickerOwners.Accounts.Count > 0)
{ reqItem["SiteOwner"] = GetUsersFromPeoplePicker(); }


protected SPFieldUserValueCollection GetUsersFomPeoplePicker(string usrType)
{
  ArrayList arrUsersMain = new ArrayList();
  ArrayList arrUsersSec = new ArrayList();
  PickerEntity UserEntity = new PickerEntity();
  SPFieldUserValueCollection fieldUsrValues = new SPFieldUserValueCollection();
  int UsrCount = 0;

  arrUsersMain = UserPickerOwners.Entities;
  UsrCount = UserPickerOwners.Accounts.Count;

  for (int j = 0; j < UsrCount; j++)
  {
    UserEntity = (PickerEntity)arrUsersMain[j];         
    string strUserLogin = UserEntity.Key;
    arrUsersSec.Add(strUserLogin);
  }      

   foreach (object obj in arrUsersSec)
   {
    string login = obj.ToString();
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
      using (SPSite elevatedSite = new SPSite(web.Site.ID))
      {
          elevatedSite.AllowUnsafeUpdates = true;
          SPWeb elevatedWeb = elevatedSite.OpenWeb(web.ID);
          {
               elevatedWeb.AllowUnsafeUpdates = true;
               SPUser User = elevatedWeb.EnsureUser(login);
               SPFieldUserValue val = new SPFieldUserValue(web, User.ID, User.Name);
               fieldUsrValues.Add(val);
           }
         }
      });
    }
   return (fieldUsrValues); 

}


"0x80040E14" or "HTTP 500" error message or Site Owner cannot edit\add content to their site

We have 2 web applications[Web App1, Web App2] running in our farm. Issue was that one Site Owner within Web App1 couldn't add or modify their content. Further investigation revealed that all sites within Web App1 threw an error like ""0x80040E14" while trying to add new content or "HTTP 500" error while trying to modify content. Web App2 was working fine. At least that part was good.

Googled the issue; Check the Database associated with the farm. You can get details about your SQL Server database used by your farm from your Central Admin.

  • Connect to the Server to check the space. 
  • OR use SQL Server Management Studio to connect to the SQL Server from your Sharepoint Server. 
When you connect through the SQL Server Management Studio, Expand Databases and look for WSS_WebAPP1_Content[ the content database used by the Web Application that has trouble]. Right Click and look for Properties. you can get an idea about the space available. In my case this was low. 

Request your SQL Server administrator to increase the server space and Reboot. 

Issue was fixed. This link was very helpful.

Leave your comments below.




Add a user to Sharepoint site using Elevated priviledges to avoid "Access denied Error" 

To add a user to a SharePoint site in order to avoid "Access Denied" error, or for cases when some high level operations needs to be performed by the person with "Member" access, use the below code.

string strUserLogin = "mylogin";
   SPSecurity.RunWithElevatedPrivileges(delegate()
   {
   using (SPSite elevatedSite = new SPSite(web.Site.ID))
   {
        elevatedSite.AllowUnsafeUpdates = true;
        SPWeb elevatedWeb = elevatedSite.OpenWeb(web.ID);
        {
                elevatedWeb.AllowUnsafeUpdates = true;
                SPUser user = elevatedWeb.EnsureUser(strUserLogin);
         }
     }
  }

Leave your comments below.

ASP.Net connect to SQL

Get data from SQL table. The related connection string can be stored in web.Config file. Data from the SQL table will be populated in the drop down.
Web.config : Within Web.Config place the connection String within <appSettings>.
<appSettings>
 <add key="ConnInfo"  value="Data Source=SQLSource;Persist Security Info=True;User ID=nnnnn;Password=vvvvv" />   
  </appSettings>
<connectionStrings/>

Aspx.cs page:
SqlConnection  conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnInfo"].ToString());
conn.Open();
if (!IsPostBack)
{
  string Query = "SELECT DISTINCT TITLE FROM V_DATA Where CATEGORY_CODE='Vendor';
  SqlCommand Vendorcomand = new SqlCommand(VendorQuery, conn);
  SqlDataAdapter Vendoradapter = new SqlDataAdapter(Vendorcomand);
  DataSet Vendords = new DataSet();
  Vendoradapter.Fill(Vendords);
  DropVendorName.DataSource = Vendords;
  DropVendorName.DataTextField = "TITLE";
  DropVendorName.DataValueField = "TITLE";
  DropVendorName.DataBind();

  DropVendorName.Items.Insert(0, "Select a Vendor");
  DropVendorName.SelectedIndex = 0;

}
Conn.close();Conn.Dispose();

Leave your comments below.

Get the Sharepoint user's additional information from Active Directory

 Get the Sharepoint user's additional information from Active Directory
Note: To add or read properties from an Active Directory group you need to have special username and password like an Active Directory admin.

 SPUser currentUser = web.CurrentUser;
string userID = currentUser.LoginName;
string newUserID = userID.Remove(0, 6).ToUpper();
// GET THE USER'S info FROM THE ACTIVE DIRECTORY
string usrPath = "LDAP://CN=" + userID.Remove(0, 6).ToUpper();
UsrPath += ",OU=UserAccounts,DC=aaaaa,DC=com";

DirectoryEntry UserInAD;
UserInAD = new DirectoryEntry(usrPath);
string UsrPhone = UserInAD.Properties["telephoneNumber"].Value.ToString();
string UserEmail = UserInAD.Properties["mail"].Value.ToString();
string MgrFromAD = UserInAD.Properties["Manager"].Value.ToString();
string managerLogin = "Company\\" + MgrFromAD.Substring(3, 7).ToLower();

Here we get Manager information from Active Directory of the currently logged in user in sharepoint.

Leave your comments below.