Archive for the ‘Object Model’ Category


If you are using a custom newform.aspx then you will face difficulties in adding attachments. There are two scenarios

  1. If you make your own infopath forms/user control then you will not be having the attachments option
  2. If you remove any columns(Default for that list) using sharepoint designer , you will have the option for attachments but it will not work.

In this case you have to go for coding.

To add an attachment to an item we will access the item which needs a file to be attached to it and we will use item.Attachments.Add() method to actually attach the file.

Add a .net FileUpload control and a button to your page and on the buttonclick event write the following code.

public void btnSubmit_Click(object sender, EventArgs e)
{
bool flag=false;
Microsoft.SharePoint.Administration.SPWebApplication webApp = this.web.Site.WebApplication;
webApp.FormDigestSettings.Enabled = false;
SPList list = web.Lists[“ACBS Client Request”];

//Upload the file on the server.
fileUpload.PostedFile.SaveAs(Request.PhysicalApplicationPath + “UploadedFiles\\” + fileUpload.FileName);


FileInfo file = new FileInfo(Request.PhysicalApplicationPath + “UploadedFiles\\” + fileUpload.FileName);

foreach (SPListItem attachItem in list.Items)
{
if (Convert.ToString(attachItem[“Request Number”]) == Convert.ToString(this.Request.QueryString[“reqno”]))
{
foreach (string fileName in attachItem.Attachments)
{
if (fileName == fileUpload.FileName)
{
flag = true;
}
}
if (flag == true)
{
lblSuccess.Text = “File with same name already exists”;
}
else
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
attachItem.Attachments.Add(file.Name, AddFile(file));
attachItem.Update();
});
lblSuccess.Text = “File Uploaded Successfully”;
}
break;
}
}
}

internal static byte[] AddFile(FileInfo file)
{
FileStream fStream = File.OpenRead(file.FullName);
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
return contents;
}

Advertisements

public string GeUserAttributeValue(SPList listCollection, string attributeName,string userName)
{
if (userName == null)
{
userName = this.web.CurrentUser.Name;
}
for (int count = 0; count
}

break;
}
}
return strDeptName;
}

“z”, “#RowsetSchema is the namespace used in the XML and hence we add it to the namespace manager.
ows_Department is the name of field in XML for the Department field in the list.


In SharePoint there is not any functionality for displaying our customized error page.Like if for data validation in event/item handler we use

properties.ErrorMessage = “Opps… Not allowed”;
properties.Cancel = true;

And validation message displayed in SharePoint error Page which looks error in application.
But with this Context you can use to redirect to any of your custom or any out of the box Page.

SPUtility.Redirect(Url, SPRedirectFlags.Default, current);

Here current is nothing but the HTTPContext object that u create in the constructor of the Event handler class


If a user have only read permission for a list or a document library and if u want him to add or update item in a list then u can use

SPSecurity.RunWithElevatedPrivileges(delegate()
{
item.Update();
}
);

This method will give the user the permission to add or update the list.


Add System.Web namespace

public class MyEventHandler: SPItemEventReceiver
{
HttpContext current;

String requestNumber;
public MyEventHandler()
{
current = HttpContext.Current;
requestNumber=Convert.ToString(this.current.Request.QueryString[“reqno”]);
}
public override void ItemAdded(SPItemEventProperties properties)
{
// code  for event handler goes here

}

Since event handler methods doesn’t have access to the page object we cant access query string and Session values.
So you can use the current object to access the query string and Session values.