Tuesday, 3 June 2014

How to Access and Update User Information List Programmatically.



To acces the hidden User Information List list of a SharePoint site (only accessible by administrators), the best way is to use the SiteUserInfoList property of the SPWeb object as in the following example:

 

// Button1- title and Internal name of User Information list

 

protected void Button1_Click(object sender, EventArgs e)

        {

 

            DataTable dtt = new DataTable();

            dtt.Columns.Add("Title ", typeof(string));

            dtt.Columns.Add("InternalName", typeof(string));

 

 

            SPSecurity.RunWithElevatedPrivileges(delegate()

            {

                using (SPSite site = new SPSite("http://abc/"))

                {

                    using (SPWeb web = site.OpenWeb())

                    {

                        SPList listUserInfo = web.SiteUserInfoList;

                        SPFieldCollection fieldColl = listUserInfo.Fields;

                        foreach (SPField field in fieldColl)

                        {

                            Console.WriteLine("Name:{0} - Type:{1}", field.Title, field.TypeAsString);

 

                            dtt.NewRow();

                            //dtt.Rows.Add(words[i], words2[i], words3[i]);

                            //dtt.Rows.Add(a, b, c, txt4,txt5,txt6,txt7,txt8,txt9,txt10);

                            dtt.Rows.Add(field.Title, field.InternalName);

                        }

 

                        GridView1.DataSource = dtt;

                        GridView1.DataBind();

                    }

                }

            });

        }

 

// Button2 – fetch record from user information list

 

 

        protected void Button2_Click(object sender, EventArgs e)

        {

 

            DataTable dt = new DataTable();

            dt.Columns.Add("ID", typeof(string));

            dt.Columns.Add("Title", typeof(string));

            dt.Columns.Add("User Name", typeof(string));

            dt.Columns.Add("Email", typeof(string));

 

 

            using (SPSite mySiteCollection = new SPSite("http://abc/"))

                {

                    using (SPWeb mySite = mySiteCollection.OpenWeb(mySiteCollection.RootWeb.ID))

                    {

                        SPList usersList = mySite.SiteUserInfoList;

 

                        SPListItemCollection oSpListCln = usersList.Items;

 

                        //iterate through all the items in itemcollection object

                        foreach (SPListItem item in oSpListCln)

                        {

                            dt.NewRow();

                           

     dt.Rows.Add(item["ID"], item["Title"], item["UserName"], item["EMail"]);

                        }

 

                        GridView2.DataSource = dt;

                        GridView2.DataBind();

                    }

                }

        }

 

 

// Button3 – update record from user information list

 
        protected void Button3_Click(object sender, EventArgs e)

        {

 

            using (SPSite mySiteCollection = new SPSite("http://abc/"))

            {

                using (SPWeb mySite = mySiteCollection.OpenWeb(mySiteCollection.RootWeb.ID))

                {

                    mySite.AllowUnsafeUpdates = true;

                    SPList usersList = mySite.SiteUserInfoList;

 

//in ID(column) have record which contain 5(value) number                   

 

SPListItem item = usersList.GetItemById(Convert.ToInt32("5"));

 

                    item["EMail"] = "abc@hotmail.com";

 

                    item.Update();

                }

            }

  }

Monday, 4 November 2013

A feature with ID has already been installed in this farm. Use the force attribute to explicitly re-install the feature.

Steps through Visual Studio 2010:

1. Go to your Features in your project.
2. Open the feature.template.xml file add AlwaysForceInstall="TRUE" to the below tag Now try to deploy the code.



Steps through PowerShell:

In PowerShell, you can use the -Force parameter of the Install-SPSolution commandlet in order to force the installation of a solution:
"Install-SPSolution -Identity -GACDeployment -Force
"

Friday, 25 October 2013

Deployed Solution containing visual web part not showing up in Custom category web part for insert on page of Sharepoint 2010.


After you deployed the solution package did you activate the Feature that adds your Web Part to the Web Part gallery (it will be in Site Collection Features).

This is done for you automatically by the deployment tools in Visual Studio but you need to do it manually when you deploy to production.

Path :==> Site Action ==> SIte Settings ==> Go to Site Collection Administration ==> select site Collection feature ==> where to active Your feature.

Thursday, 30 May 2013

Custom Error page in Event Receivers for Sharepoint 2010

Following steps for Custom Error Page.

1. Update the Event Receiver: ItemAdding Section's Code would be:

public override void ItemAdding(SPItemEventProperties properties)
       {
          base.ItemAdding(properties);

           if (!properties.AfterUrl.EndsWith("pdf"))
           {
               properties.Status=SPEventReceiverStatus.CancelWithRedirectUrl;
               properties.RedirectUrl = "/_Layouts/ERTest/ApplicationPage1.aspx";
           }
       }
2. Add a Application Page to the Project, optionally a Icon to represent Error.


3. Update the Error page: Under the PlaceHolderMain Place some content to give descriptive Error message:



See the Result in action: SharePoint 2010 event receiver custom error page.



Wednesday, 29 May 2013

Restict File Type in Sharepoint Document Library Using Event Receiver.

Creating Event receivers in SharePoint 2010 is very easy With Visual Studio 2010. Here are the steps:

1. Create a New Visual Studio Project, Choose SharePoint >> 2010 >> Event Receiver >> Give it a Name. Say "EventReceiver1"



2. Make it as a Farm solution, choose the event receiver properties as in the below screen.






3. On clicking "Finish" button, Visual Studio will create the project structure as and Now, in the Elements.xml file, change the ListTemplateID="101" (Which means all document Libraries) to ListURL="RelativePathOfDocLibrary" say: "CV".

Change the <Receivers attribute



So, The complete code would be:using System;

using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace ERTest.EventReceiver1
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class EventReceiver1 : SPItemEventReceiver
    {
       /// <summary>
       /// An item is being added.
       /// </summary>
       public override void ItemAdding(SPItemEventProperties properties)
       {
           base.ItemAdding(properties);
           if (!properties.AfterUrl.EndsWith("pdf"))
           {
               properties.ErrorMessage = "You are allowed to upload only jpg Images!";
               properties.Status = SPEventReceiverStatus.CancelWithError;
               properties.Cancel = true;

              

           }

       }


    }
}

See the the result in action!SharePoint document library restricts file type other than PDF.





Friday, 24 May 2013

SharePoint Designer - Send the URL of the current item with a workflow.


So I opened SharePoint Designer and start creating the workflow associated to the list.
image
image
As there is some Field from source related to current item URL I start using it
  • Encoded Absolute URL – this one should be the one to use
  • Server Relative URL
  • URL Path
image
Unfortunately, none of these options were providing the correct link. Indeed, these options are providing an incorrect path:
Encoded Absolute URL
http://webappurl/Lists/Demo/1_.000
Server Relative URL
/Lists/Demo/1_.000
URL Path
/Lists/Demo/1_.000

As you can see, the item URL is composed by an ID while it should be http://webappurl/Lists/Demo/dispform.aspx?id=[%Current Item:ID%]
So, to obtain the correct item URL, you must use the following Lookup
  • Data source: Workflow Context
  • Field from source: Current Item URL
image

If you want your workflow start automatically when item added in a list.

Note: When using a Document Library IT Give a directly on File(Document) Link.





Tuesday, 7 May 2013

Upload A document to a Document Library through programmatically...


Note: I used Visual Webpart which template in visual studio for sharepoint.

protected void btnUpload_Click(object sender, EventArgs e)
        {
            BindDataInSPList();
        }
        private void BindDataInSPList()
        {
            try
            {
                SPSite oSite = SPContext.Current.Site;
                SPWeb oWeb = oSite.OpenWeb();

                oWeb.AllowUnsafeUpdates = true;
                SPDocumentLibrary mylist = oWeb.Lists["CV"] as SPDocumentLibrary;

                string fileName = System.IO.Path.GetFileName(FileUpLoadDoc.PostedFile.FileName);
                //Get file extension ( *.doc OR *.docx )
                string fileExtension = FileUpLoadDoc.FileName.Substring(FileUpLoadDoc.FileName.IndexOf("."));
                byte[] fileBytes = FileUpLoadDoc.FileBytes;
                string destUrl = mylist.RootFolder.Url + "/" + fileName;
                SPFile destFile = mylist.RootFolder.Files.Add(destUrl, fileBytes, true);
                destFile.Update();
                oWeb.AllowUnsafeUpdates = true;
                //update metadata
                destFile.Item["Name"] = txtSTDFNNAme.Text;
                //destFile.Item["Address"] = txtAddress.Text;

                destFile.Item.Update();
            }
            catch (Exception ex)
            { ; }
        }