Archive for the ‘Object Model’ Category


In an Item Adding event handler Properties.ListItem will be null. Same is the case with Properties.BeforeProperties & Properties.AfterProperties too. So how to get the values entered in the NewForm.aspx in the ItemAdding method. Here we go..

You can access the value using AfterProperties property. But here you have to pass the internal name of the column whose value you want to access.

properties.AfterProperties[“Internal Name of the column”]

Or in case if you don’t know/want to explicitly specify the internal name you can get the same using Field.InternalName property. The below code snippet I used explains the same.

typeName  = Convert.ToString(site.Lists[properties.ListId].Fields[“Type Of Shoes”].InternalName);
allotedShoeType = Convert.ToString(properties.AfterProperties[typeName]);

Hope it helps..


Advertisements

I was working with the People Editor Control and realized there are a couple weird things you need to do before you can read values into the control and save values into a list.  Here is a samples related to saving multiple/single user details into a people or groups column in a list.


SPFieldUserValueCollection UserCollection = new SPFieldUserValueCollection();
string[] UsersSeperated = pplEditor.CommaSeparatedAccounts.Split(‘,’);
if (UsersSeperated.Length > 0)
{
foreach (string UserSeperated in UsersSeperated)
{
SPUser User = web.SiteUsers[UserSeperated];
SPFieldUserValue UserName = new SPFieldUserValue(web, User.ID, User.LoginName);
UserCollection.Add(UserName);
}

item[“Name”]=UserCollection

This way we can store single or multiple user names in a People or Groups column from a people editor group.


To get the usage details of the site we use the method site.GetUsage(SPUsageReportType , SPUsagePeriodType ).

SPUsageReportType Enumeration

The SPUsageReportType enumeration specifies the type of information returned in a usage report for a SharePoint site.

The following table shows the members of the SPUsageReportType enumeration and a brief description

Name Description
browser The type of Web browser used to visit the SharePoint site. All usage data refers specifically to visits from referring URLs external to the site.
os The operating system used on the client computer. All usage data refers specifically to visits from referring URLs external to the site.
refUr External URLs through which users navigated to the SharePoint site.
url URLs of pages that are visited or of pages for lists that are updated. Discussions about a page are counted as hits on that page.
user Users who visited the site.

SPUsagePeriodType Enumeration

The SPUsagePeriodType enumeration specifies the time interval on which a usage report for a Web site is based.

The following table shows the members of the SPUsagePeriodType enumeration and a brief description

Name Description
day Returns usage information for each day during the past 31 days starting from the previous day
lastMonth Summarizes usage information for the last 31 days relative to the previous day

Below are details how we can access respective data.

Accessing User daily report

//Users who visited the site
DGUsers.DataSource = site.GetUsageData(SPUsageReportType.user, SPUsagePeriodType.day);

Accessing User monthly report

//Users who visited the site
DGUsers.DataSource = site.GetUsageData(SPUsageReportType.user, SPUsagePeriodType.lastMonth);

Accessing Browser daily report

//The type of browsers used to visit the site
DGBrowser.DataSource = site.GetUsageData(SPUsageReportType.browser, SPUsagePeriodType.day);

Accessing Browser monthly report

//The type of browsers used to visit the site
DGBrowser.DataSource = site.GetUsageData(SPUsageReportType.browser, SPUsagePeriodType.lastMonth);

Accessing Operating System daily report

//The Operating System used in client computer
DGOs.DataSource = site.GetUsageData(SPUsageReportType.os, SPUsagePeriodType.day);

Accessing Operating System monthly report

//The Operating System used in client computer
DGOs.DataSource = site.GetUsageData(SPUsageReportType.os, SPUsagePeriodType.lastMonth);

Accessing refUrl daily report

//External URL client used to navigate to SharePoint site
DGRefUrl.DataSource = site.GetUsageData(SPUsageReportType.refUrl, SPUsagePeriodType.day);

Accessing refUrl monthly report

//External URL client used to navigate to SharePoint site
DGRefUrl.DataSource = site.GetUsageData(SPUsageReportType.refUrl, SPUsagePeriodType.lastMonth);

Accessing url daily report

//URL’s of pages visited
UsageGrid.DataSource = site.GetUsageData(SPUsageReportType.url, SPUsagePeriodType.day);

Accessing url monthly report

//URL’s of pages visited
UsageGrid.DataSource = site.GetUsageData(SPUsageReportType.url, SPUsagePeriodType.lastMonth);

Impersonating a user

Posted: October 21, 2008 in Impersonation, Object Model

If a user have read only permission for a list and in case there is a need for that user to add or update the list item, then he can be given the permission to add/update using code only for the time of updating.

Here AppSettings[“DomainAdmin”] is the entry in web.config which gives the admin ID of the site. This can be anyother ID having write permissions.

protected SPList ImpersonateUser(string strListName)
{
this.web = SPContext.Current.Web;
SPList listCollection;
string domainAdmin = System.Configuration.ConfigurationManager.AppSettings[“DomainAdmin”];
using (WindowsImpersonationContext impctx = WindowsIdentity.Impersonate(IntPtr.Zero))
{
SPUser admin = web.AllUsers[domainAdmin];
using (SPSite adminSite = new SPSite(web.Site.Url.ToString(), admin.UserToken))
{
using (SPWeb adminWeb = adminSite.OpenWeb())
{
listCollection = adminWeb.Lists[strListName];
}
}
}
return listCollection;
}

Now you can use the list returned by this method to update the item. Its better to use

SPSecurity.RunWithElevatedPrivileges(delegate()
{ });


foreach (SPListItem attachItem in list.Items)
{
if (Convert.ToString(attachItem[“Request Number”]) == Convert.ToString(this.Page.Request.QueryString[“reqno”]))
{
foreach (string fileName in attachItem.Attachments)
{
SPFile file = attachItem.ParentList.ParentWeb.GetFile(attachItem.Attachments.UrlPrefix + fileName);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.AddAttribute(HtmlTextWriterAttribute.Target, “_blank”);
writer.AddAttribute(HtmlTextWriterAttribute.Href, file.ServerRelativeUrl);
writer.AddStyleAttribute(HtmlTextWriterStyle.FontSize, “12px”);
writer.RenderBeginTag(HtmlTextWriterTag.A);
writer.Write(file.Name);
writer.RenderEndTag();
flag = true;
}
break;
}
}