Thursday, April 18, 2013

Sharepoint 2010: Enable Anonymous: All The Errors And Problems (i hope)

so i started working is SP, yey me!!
yesterday we wanted to set the Anonymous Extension and it didnt work!!
so thx to our DEAR Team Leaders Eran and Assaf!!

the begining - u dont need me for that, so many tutorials lets just sum it:
Central Admin, Manage Web App, Authentication Providers, Default, Enable Anonymous Access.
Anonymous Policy - No Policy.

the issue: Site Settings => Site Permissions => Anonymous Access: you dont see it!!
solutions:
1. are you on your admin site or on the anonymous site? you can only set and see that on the anonymous site.
2. hey wait - i am on the anonymous site and cant get in to the administrative part! cant log in!, well that obvious, its anonymous access, go to IIS, ur site, Authentication and enable Windows Authentication. dont forget to disable that in the end.
3. copy ur admin web.config to ur anonymous site web.config, there r many permissions there that dont let u do administrative stuff. later learn that and harden security, or save the old web.config somewhere to bring it back later. *Most likely what was missins is TrustLevel=Full, so best practice is to make sure what exactly was missing, in our case i just copied everything.

the issue: working with bin, site not working, though can go to administrative pages (if u write the urls and allow win auth).
solutions:
1. dont forget to copy Dlls to Anonymous and Admin application's bin directory. after all u r wokring with the bin :D.

the issue: cant see logs. i am assuming u already did the errors in the Central Admin, on both drop downs.
solutions:
1. Start => Administrative Tools => Active Directory Users And Computers => Sharepiont Users, make sure all users u use for SP like sp_farm ect. all have the same permissions, just double click them and go to Member Of tab and make sure everybody have everything.
if u want the specifics then the thing is that the application pool user of ur SP app belongs to the same groups as the farm admin.
2. do the Central Admin thing again.
3. if it doesnt helps try to restart the Sharepoint Tracing Service and iisreset and try again.

the issue: ur in SP2013 and u did everything yet ur anonymous leg still want authentication


the issue: SP2013 and u get 401
go to all the "hot" libraries like style library and go to library permissions and if they have unique permissions delete it and restore to inherit. usually u'll see the "bad kids" in an alert in site permissions.
another library that he wont tell u is Master Page and Page Layout Gallery (from sitesettings)

P.S.
did the resources got copied to the new site's libraries?

Friday, April 5, 2013

Jquery UI autocomplete with Ajax: master

stage 4: master
sometimes you would like to do more, usually with the _renderItem, lets see that

$(function () {
  $.ui.autocomplete.prototype._renderItem = function (ul, item) {
    return $("<li></li>")
      .append("<a>" + item.name + " <br><span style='background-color:blue'> " +  item.time + "</span><br>" + item.id + "</a>")
      .append("<a href='default.aspx'>go go go</a>")
      .appendTo(ul);
  };
 
           
  $("#MyTextBox").autocomplete({
    source: function (request, response) {
      $.ajax({
       
url: "Handler1.ashx",
        data: "Name=" + request.term,
        success: function (data) {
          response($.map(data, function (item) {
            return {
              name: item.Name,
              id: item.ID,
              time: item.CreationTime
            }
          }))
        }
      })
    }

  });
});
 


usually u would use _renderItem when you want to costumize how the ul looks, and thats what i gave in the expamle, u can even click the 2nd <a> to move a page.
in that case btw you r not limited to the default of 'label' ect.
ul is the ul dom element u see when the autocomplete is being opened, and item is the item you put in the response so u can skip the map part and your js can look like that:

$(function () {
  $.ui.autocomplete.prototype._renderItem = function (ul, item) {
    return $("<li></li>")
       .append("<a>" + item.Name + " <br><span style='background-color:blue'> " + item.CreationTime + "</span><br>" + item.ID + "</a>")
       .append("<a href='default.aspx'>go go go</a>")
       .appendTo(ul);
  };
 
           
  $("#MyTextBox").autocomplete({
    source: function (request, response) {
      $.ajax({                              
        url: "Handler1.ashx",
        data: "Name=" + request.term,
        success: function (data) {
          response(data)
        }
      })
    },

    select: function (event, ui) {
      $("#Blue").text(ui.item.Name);
      $("#Green").text(ui.item.ID);
      $("#Orange").text(ui.item.CreationTime);
    }
 
});
});
 


hope u had fun, now if u feel GrandMaster go check the examples they have in jquery UI site, autocomplete examples.

Jquery UI autocomplete with Ajax: the advanced stage

stage 3: advanced
so we have a few cool functions with this autocomplete.
1st thing is that we can return complex data, say we return an object:

class AnObject
{
 
public int ID { get; set; }
  public string Name { get; set; }
  public DateTime CreationTime { get; set; }
}

public void ProcessRequest(HttpContext context)
{
 
List<AnObject> l = new List<AnObject>();
  l.Add(new AnObject() { ID = 1, Name = "space marine", CreationTime = DateTime.Now });
  l.Add(new AnObject() { ID = 2, Name = "galaxy cow", CreationTime = DateTime.UtcNow });
  l.Add(new AnObject() { ID = 3, Name = "dimention cat", CreationTime = DateTime.Today });
  l.Add(new AnObject() { ID = 4, Name = "big bad zerg", CreationTime = DateTime.Now.AddDays(3) });
  l.Add(new AnObject() { ID = 5, Name = "psychic being", CreationTime = DateTime.Now.AddDays(-5) });

  if (context.Request.QueryString["ID"] != null)
    l = l.Where(o => o.ID == int.Parse(context.Request.QueryString["ID"])).ToList();

  if (context.Request.QueryString["Name"] != null)
    l = l.Where(o => o.Name.Contains(context.Request.QueryString["Name"])).ToList();

  context.Response.ContentType = "application/json";
  JavaScriptSerializer jss = new JavaScriptSerializer();
  context.Response.Write(jss.Serialize(l));

} 

client side add this:
 
url: "Handler1.ashx",
data: "Name=" + request.term,
success: function (data) {
  response($.map(data, function (item) {
    return { label: item.ID + " " + item.Name + " " + item.CreationTime }
  }))
}
 


as you can see we just brought back complex data that the map function knows it in the item object and you can spam it in your choise.

something a bit more useful is the select function, say we put this html
<input id="MyTextBox" type="text" />
<div id="Blue" style="background-color:Blue"></div>
<div id="Green" style="background-color:Green"></div>
<div id="Orange" style="background-color:Orange"></div>

we can do this
$("#MyTextBox").autocomplete({
  source: function (request, response) {
     $.ajax({
      
url: "Handler1.ashx",
       data: "Name=" + request.term,
       success: function (data) {
         response($.map(data, function (item) {
           return {
             label: item.Name,
             id: item.ID,
             time: item.CreationTime
           }
         }))
       }
     })
   },
   select: function (event, ui) {
      $("#Blue").text(ui.item.label);
      $("#Green").text(ui.item.id);
      $("#Orange").text(ui.item.time);
   }
});
 


what happened here? well $.map just creates a new array of objects as we decide it, do we can put anything there (btw if u return a 'label' prop in your object, like in 'AnObject', you could just do response(data) and thats it cuz 'label' is used to show).
now the response is a callback thing and as u can see we can use is afterwards in our events like select there we can use this object as ui.item.

more functions and events in their api, a great api, no simple walkthrough
for the final part for masters!
http://bresleveloper.blogspot.co.il/2013/04/jquery-ui-autocomplete-with-ajax-master.html

Jquery UI autocomplete with Ajax: the novice stage

stage 2: novice
we will try to understand that thing a bit with our own server side.
add new item, choose Generic Handle, th filename will be Handler1.ashx.
paste this:
public void ProcessRequest(HttpContext context)
{
 
context.Response.ContentType = "text/plain";
  JavaScriptSerializer jss = new JavaScriptSerializer();
  var x = new string[] { "ariel", "avraham", "aviel", "eliyahu", "eliezer" };
  context.Response.Write(jss.Serialize(x));
}

client side paste this
$(function () {
  $("#MyTextBox").autocomplete({ source: "Handler1.ashx" });
});

yep, the autocomplete source can get a pure handler. try it and see that we now have our own handler, now lets make it ajax with server side filter, start with asking for the same data but with the filter function
$("#MyTextBox").autocomplete({
  source: function (request, response) {
    $.ajax({
     
url: "Handler1.ashx",
      success: function (data) {
        response($.map(data, function (item) {
          return { label: item }
        }))
      }
   })
  }
});
 


so how do we filter that? the request object have a thing call request.term, thats what the user put in the textbox, so we want to send it to our handler, so with using data{} on the ajax call we just send parameters to the QueryString on the server side so:

context.Response.ContentType = "application/json";
JavaScriptSerializer jss = new JavaScriptSerializer();
var filter = context.Request.QueryString["Filter"];
var x = new string[] { "ariel", "avraham", "aviel", "eliyahu", "eliezer" };
var response = jss.Serialize(x.Where(s => s.Contains(filter)));
context.Response.Write(response);

and client side

$.ajax({
 
url: "Handler1.ashx",
  data: "Filter=" + request.term,
  success: function (data) {
    response($.map(data, function (item) {
      return { label: item }
    }))
  }
})
 


and BANG it does the magic! so here we learned how to make both server-side and cliend side simple autocomplete with ajax requests.

next some nice things we can do with all that except autocomplete
http://bresleveloper.blogspot.co.il/2013/04/jquery-ui-autocomplete-with-ajax_5.html
 

Jquery UI autocomplete with Ajax: the idiotproof to master full guide

i decided to write this post cuz this thing tool my like an hour or 2 to get, and while looking at my solution i saw i should have got it in 2 seconds, so like other places sometime the examles are just not simple enough, so lets go.

stage 1: idiot proof
i'll do everything from the begining, download jquery ui 1.9.2 from their site. i opened an empty asp.net web app, added the jquery-ui-1.9.2.custom.js and the jquery-1.8.3.js and write html:

<input id="MyTextBox" type="text" />
and then JS:
$(function () {
  $("#MyTextBox").autocomplete({
    source: [ "ariel", "avraham", "aviel", "eliyahu", "eliezer" ]
  });
});
 


and run, thas it u have a working auto complete. now in order to have my array from a request i could just do this:
$.ajax({
 
url: "default.aspx/BringMeListFromWebMethod",
  contentType: "application/json; charset=utf-8",
  type: "POST",
  async: false,
  success: function (data) { arr = data.d; }
});
$("#MyTextBox").autocomplete({  source: arr }); 

of course the BringMeListFromWebMethod is a WebMethod that return the same string[].
for any1 who need some instructions about basics of ajax or WebMehtods:
http://bresleveloper.blogspot.co.il/2012/04/ajax-3-simle-ways.html

but then we want to already do the filtering server side, otherwise whats the point? so in order to make it simple we will use what Jquery UI gives us for free,
$("#MyTextBox").autocomplete({
  source: function (request, response) {
    $.ajax({
     
url: http://ws.geonames.org/searchJSON,
      dataType: "jsonp",
      data: { name_startsWith: request.term },
     
success: function (data) {
        response($.map(data.geonames, function (item) {
          return { label: item.name }
        }))
      }
    })
  }
});
 


i'll put it up to you to call the right ajax thing for you app and make sure its json, its a problem to use webmethod, easier to make a handler or something and u need to json serialize it.

but still a little overview: the url is a ajax handler that returns a large array in json.
the data sends the request.term, that what you put in the textbox, to the right parameter.
lastly the label:item.name, that creates the list you will see.

join us again in the novice stage here
http://bresleveloper.blogspot.co.il/2013/04/jquery-ui-autocomplete-with-ajax-novice.html