Wednesday, 3 August 2011

Admin SVC must be running in order to create deployment timer job

Issue : Recently, while using a PowerShell command (Update-SPSolution) I encountered below error.

Admin SVC must be running in order to create deployment timer job

Solution : You would encounter above error if “ SharePoint 2010 Administration” services is nor running. So to resolve above issue goto run -> type “services.msc” and start “SharePoint 2010 Administration” service.

Friday, 1 July 2011

Multiple list instances of list definition

Problem:

Recently, I observed one weird thing in my SharePoint application.I was creating a list definition which had multiple views. I wanted to create one instance of the site definition but I could see multiple list instances of the same list in my site.

Reason:

I figured out the reason behind this issue.

In schema.xml under the views node I had multiple views definitions.
I had created one view and replicated the same for rest of the views.

<View DisplayName="By Customer Name" DefaultView="TRUE" BaseViewID="1" Type="HTML" MobileView="TRUE" MobileDefaultView="TRUE" ImageUrl="/_layouts/images/generic.png" XslLink="main.xsl" WebPartZoneID="Main" Url="By Customer Name.aspx" SetupPath="pages\viewpage.aspx">
<Toolbar Type="Standard" />
<XslLink>main.xsl</XslLink>
<Query>
</Query>
<ViewFields>
</ViewFields>
<RowLimit Paged="TRUE">100</RowLimit>
</View>


I was modifying the query and fieldrefs. Erroneously DefaultView="TRUE" attribute was present in all the views, which was resposible for creating multiple instances of the list and keeping that particular view as the default view.

Sunday, 19 June 2011

How to programmatically download attachments from list items

To download an attachment, you must first find the download link and then redirect to another page.

The ending of the response from current page does not affect the functionalities on the second one.


using (SPSite site = new SPSite("http://SiteCollectionURL"))
{
using (SPWeb web = site.OpenWeb())
{
string file = string.Empty;
SPList list = web.Lists["MyList"];
SPListItem currentItem = myList.GetItemById(id);
if (currentItem["AttachmentName"] != null)
{
file = "/Lists/ MyList/Attachments/" +
id.ToString() + "/" +
currentItem["AttachmentName"].ToString();
System.Web.HttpContext.Current.Session["FileName"] =
currentItem["AttachmentName"].ToString();
System.Web.HttpContext.Current.Session["Attachment"] =
file.Trim();
}
else
{
lblReport.Text = "No File name found";
}
if (file != string.Empty)
{
Reponse.Redirect("download.aspx");
}
}
}

On the download.aspx page, you need to the code shown below to download the file.

if (System.Web.HttpContext.Current.Session["Attachment"] != null)
{
string strName = System.Web.HttpContext.Current.Session["FileName"].ToString();
string sbURL = System.Web.HttpContext.Current.Session["Attachment"].ToString();
System.Web.HttpResponse response;
response = System.Web.HttpContext.Current.Response;
System.Web.HttpContext.Current.Response.ContentEncoding =
System.Text.Encoding.Default;
response.AppendHeader("Content-disposition", "attachment; filename=" + strName);
response.AppendHeader("Pragma", "cache");
response.AppendHeader("Cache-control", "private");
response.Redirect(sbURL);
response.End();
}

Deleting an Attachment from SPList

Use below method to delete attachment of specific list item.

public void DeleteAttachment(int id,string fileName)
{
using (SPSite site = new SPSite("http://SiteCollectionURL"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["MyListName"];
SPListItem delItem = list.GetItemById(id);
SPAttachmentCollection files = delItem.Attachments;
files.Delete(fileName);
delItem.Update();
}
}
}

Adding an Attachment to an Item

Below code can be used to add attachment to list item.

using (SPSite site = new SPSite("http://SiteCollectionURL"))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["MyListName"];
SPListItem newItem = list.GetItemById(id);
byte[] contents = null;
if (fileUpload.PostedFile != null && fileUpload.HasFile)
{
using (Stream fileStream = fileUpload.PostedFile.InputStream)
{
contents = new byte[fileStream.Length];
fileStream.Read(contents, 0, (int) fileStream.Length);
fileStream.Close();
}
SPAttachmentCollection attachments = newItem.Attachments;
string fileName = Path.GetFileName(fileUpload.PostedFile.FileName);
attachments.Add(fileName, contents);
newItem.Update();
}
}
}

Saturday, 18 June 2011

How to check if User is Present in a Group

You can add extension method to SPUser class to achieve this requirement.

Add the below namespace.

using System.Linq;


public static bool UserExist(this SPUser user, string groupName)
{
return user.Groups.Cast().Any(gn => gn.Name == groupName);
}

How to close the sharepoint modal dialog and refresh the parent page.

Recently, I stumbled upon a situation where I had Calendar list and I had provided custom save and cancel button.

In List setting I had kept "Launch forms in a dialog" option to "No" so that my NewForm/EditForm/DispForm can open on same page instead of a dialog window.

But in case of Calendar View Edit and Display form always opens in Dialog.So after updating the record we explicitly need to close the dialog and redirect to any view.

At server side I tried below code.

Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
Context.Response.Flush();
Context.Response.End();

But It didn't worke!!!! So I tried below approach and it worked.

Since I had to close the window only user is editing form Calendar view.So wherever you open a dialog a querystring is passed with name "IsDlg" and value ="1". So If ""IsDlg" is present in the URL redirect the user to your custom page from server side, and use below javascript to close the dialog.

Server Side Code


if (Request.QueryString["IsDlg"] != null)
{
dlgID = Request.QueryString["IsDlg"].ToString();
}
if (!string.IsNullOrEmpty(dlgID))
{
Response.Redirect(SPContext.Current.Web.Url + "/Default.aspx?Close=1");
}


Write below code in Default.aspx or your custom defualt page.

var closeFlag =location.search.substring(1).split("Close=");
if(closeFlag !="")
{
var flag = closeFlag[1].split("&");
if(flag=="1")
{
Close();
}
}

function Close()
{
// Will close the window and update the view.
window.frameElement.commitPopup();
}
</script>

Query List/Document Library in Specific Folder

To query SharePoint List or Document Library in specific Folder “ FolderServerRelativeUrl ” as part of the CAML Query Code Snippet ...